Error while implementing django models . How to debug? - django-models

Error: One or more models did not validate:
maps.parking: 'layer_id' has a relation with model sdr_layer.Sdr_layer, which has either not been installed or is abstract.
This is the error that I have .
My maps/models.py looks like this
# This is an auto-generated Django model module created by ogrinspect.
from django.contrib.gis.db import models
class Parking(models.Model):
layer_id= models.ForeignKey(sdr_layer.Sdr_layer)
name = models.CharField(max_length=80)
descriptio = models.CharField(max_length=80)
geom = models.PointField(srid=4326)
objects = models.GeoManager()
# Auto-generated `LayerMapping` dictionary for Parking model
parking_mapping = {
'name' : 'Name',
'descriptio' : 'Descriptio',
'geom' : 'POINT25D',
}
Where sdr_layer/models.py looks like this
from django.db import models
class Sdr_Layer(models.Model):
layer_name = models.CharField(max_length = 100)
layer_attribute_name = models.CharField(max_length = 100)
I basically want the Sdr_layer.id to act as the foreign key for the app maps .

Make sure sdr_layer is listed your INSTALLED_APPS in settings.py

I think you intended models.ForeignKey(sdr_layer.Sdr_Layer).

Related

URL with dot in Django Rest Framework

I have the following model with a primary_key=True specified:
class Team(models.Model):
name = models.CharField(
max_length=64,
primary_key=True,
)
... other fields
When I serialize this model, I do the following:
class TeamSerializer(serializers.ModelSerializer):
class Meta:
model = Team
fields = ('url', 'name',) # and another fields
My viewset:
class TeamViewSet(viewsets.ModelViewSet):
lookup_value_regex = '[-\w.]'
queryset = Team.objects.all()
serializer_class = TeamSerializer
filter_fields = ('name',) # and another fields
My urls.py:
router = routers.DefaultRouter()
router.register(r'teams', TeamViewSet)
urlpatterns = [
url(r'^api/', include(router.urls)),
# I am not sure if this url is right. I repeat of include(router.urls)
url(r'^api/teams/(?P<name>[-\w.]+)/', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
Then, when I create a Team object with name attribute containing dot ., for example Latinos F.C. and I go to the rest url, I get:
I am not sure about of how to use the lookup_value_regex attribute in my viewset. In this answer is used with some basic regex, but if I use it, any Team object is reachable via my serialized Rest API.
How to can I get a url like as: /api/teams/Name.F.C. in my serialized Team model?
First of all check if you have set APPEND_SLASH to True in your settings, because if not - the missing slash (at the end of the URL) is a problem.
Second - I do not think that dot is a problem, the problem can be a space - coded as %20;
Third - such urls looks just ugly :) You should consider changing it to some kind of a slugs: Latinos F.C. -> latinos-fc;
If you do that (just add additional field on the model with slug - this field should be obviously unique) - set up the lookup_field on your view - this will solve your problem.
Consider the example:
views.py
class SomeViewSet(viewsets.ModelViewSet):
queryset = SomeModel.objects.all()
serializer_class = SomeSerializer
lookup_field = 'slug_name'
serializers.py
class SomeSerializer(serializers.ModelSerializer):
class Meta:
model = SomeModel
fields = ('id', 'name', 'slug_name')
read_only_fields = ('slug_name',)
def to_internal_value(self, data):
ret = super(SomeSerializer, self).to_internal_value(data)
ret['slug_name'] = slugify(ret['name'])
return ret
models.py
class SomeModel(models.Model):
name = models.CharField(max_length=100)
slug_name = models.SlugField(unique=True, max_length=100)
urls.py
router.register(r'teams', SomeViewSet, base_name='teams')
urlpatterns = router.urls
And now:
creation:
details:
Can you do that this way? Or you really need the dots?

Database integrity issue with multi-table inheritance in django

Here are the relevant models:
class Event(models.Model):
objects = InheritanceManager()
game = models.ForeignKey(Game)
time = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['time']
class ShipMoveEvent(Event):
objects = InheritanceManager()
ship = models.ForeignKey(Ship)
space = space(null=True, blank=True)
class DepthChargeEvent(ShipMoveEvent):
target = space()
nearMiss = models.PositiveSmallIntegerField()
hit = models.ForeignKey(Sub, blank=True, null=True)
I can create a ShipMoveEvent fine. When I go to create the DepthChargeEvent, I get this error on save:
django.db.utils.IntegrityError: NOT NULL constraint failed: sub_search_depthchargeevent.event_ptr_id
Any ideas why? I am stumped. It obviously has something to do with django's multi-table inheritance.
It seems that this is a bug in the sqlite database binding. I switched to postgresql and have not had this problem since.

How to create an entity of a model with referenceProperty without the reference exists?

I have two model classes Team and Student. Each Team entity has more than one Users entities, I think "one to many" is a proper model. I code like this:
class Team(db.Model):
teamNmae = db.StringProperty(required=True)
teamID = db.IntegerProperty(required=True)
class Users(db.Model):
name = db.StringProperty(required=True)
reference = db.ReferenceProperty(Team,collection_name=teamMembers)
Follow this document, I need to first create a Team entity, then create Users whose reference is set to this Team entity. But,here is the problem, I want to create User first, then User can create a Team. So, how to set reference when I create a User that no Team exists ?
EDIT1:
I write simple code to test if I can create a entity without its reference entity created.
_author__ = 'Steven_yang'
import os
import webapp2
from google.appengine.ext import db
from google.appengine.ext.webapp import template
defaultStu = [
'qingWANG',
'stevenYANG',
'jingZHU',
'conghuiHE',
'lianDUAN',
'xinHAO'
]
class Students(db.Model):
#reference = db.ReferenceProperty(Team,collection_name='teamMember',required=False)
name = db.StringProperty(required=True)
class Team(db.Model):
teamName = db.StringProperty(required=True)
teamID = db.IntegerProperty(required=True)
def createStu():
stu_count = Students.all().count(1)
if stu_count == 0:
for stu in defaultStu:
newStu = Students(name = stu)
newStu.put()
class testReferenceHandler(webapp2.RequestHandler):
def get(self):
createStu()
self.render_page()
def render_page(self):
stus = Students.all().fetch(10)
templateValues={}
templateValues['stus'] = stus
form = os.path.join(os.path.dirname(__file__),'template/testref.html')
renderForm = template.render(form,templateValues)
self.response.out.write(renderForm)
app = webapp2.WSGIApplication([('/testref',testReferenceHandler)],debug=True)
HTML file is:
<body>
{% for s in stus%}
<div>{{s.name}}</div>
{% endfor%}
</body>
</html>
when I comment out the ReferenceProperty line, everything is fine. When I add the ReferenceProperty line, I got a server error. So, I can't create a entity without its reference created before?
Did you try to set reference to Team model as None when creating a User instance?
Also change the last line your code snippet to:
reference = db.ReferenceProperty(Team,required=False)
i.e. add required=False. This is what is suggested in this answer.

Using GeoDjango model as an abstract class

I'm playing with GeoDjango and have some doubts. I'll really appreciate any comment and suggestion.
This is my problem. First, I've defined this (abstract) class:
from django.contrib.gis.db import models
from django.contrib.gis.geos import *
class LocatableModel(models.Model):
country = models.CharField(max_length=48, blank=True)
country_code = models.CharField(max_length=2, blank=True)
locality = models.CharField(max_length=48, blank=True)
sub_locality = models.CharField(max_length=48, blank=True)
street = models.CharField(max_length=48, blank=True)
address = models.CharField(max_length=120, blank=True)
point = models.PointField(null=True)
objects = models.GeoManager()
class Meta:
abstract = True
Second, I've defined this other 'Entity' class, which
represents a person or organization related to my site:
from django.db import models
class Entity(models.Model):
name = models.CharField(max_length=64)
slug = models.SlugField(max_length=64, unique=True)
website = models.URLField(verify_exists=False, blank=True)
email = models.EmailField(blank=True)
...
Finally, I've created a class from the previous ones:
import LocatableModel
import Entity
class Organization(Entity, LocatableModel):
timetable = models.CharField(max_length=64)
...
In my views, I'd like to find organizations near a specific point:
from django.contrib.gis.geos import Point
from django.contrib.gis.measure import D
def index(request):
pnt = Point(12.4604, 43.9420)
dic = { 'orgs': Organization.objects.filter(point__distance__lte=(pnt, D(km=7))) }
return render_to_response('index.html', dic)
But I receive the error:
"Join on field 'point' not permitted. Did you misspell 'distance' for
the lookup type?"
I think I'm doing a mess with the model 'objects' property, but I'm not sure. Any ideas?
Thanks in advance.
This error has been seen before, and claimed to be solved in this ticket 3 years ago:
https://code.djangoproject.com/ticket/9364
When I ran into this same problem, I noticed in the ticket that the query manager was set explicitly to GeoManager in the inherited model(s). So adding a line like,
class Organization(Entity, LocatableModel):
timetable = models.CharField(max_length=64)
...
objects = models.GeoManager()
...may solve the issue you're seeing, it worked for me.

Relationships in Django Admin

I get really confused with many-to-many database relationships, so can some one please clarify how I would achieve this?
I need a table of "Tags" (as in tag words) and a table for "Entries", such at many "Entries" could correspond to many Tag words.
Right now I have my models like this:
# models.py
class Tags(models.Model):
tag = models.CharField(max_length=255)
entry = models.ManyToManyField(Entry)
class Entry(models.Model):
entry = models.CharField(max_length=255)
description = models.TextField()
Now I'm confused, how would I setup my admin.py so I could then add tags when I create a new entry?
What you need is using the through feature of models:
class Tag(models.Model):
tag = models.CharField(max_length=255)
entry = models.ManyToManyField(Entry, through='TaggedEntries')
class Entry(models.Model):
entry = models.CharField(max_length=255)
description = models.TextField()
class TaggedEntries(models.Model):
entry = models.ForeignKey(Entry)
tag = models.ForeignKey(Tag)
and now use that model in your admin:
class TagsInline(admin.TabularInline):
model = TaggedEntries
extra = 1
class EntryAdmin(admin.ModelAdmin):
inlines = (TagsInline, )
admin.site.register(Entry, EntryAdmin)
admin.site.register(Tag)
You will need something along the lines of:
# admin.py
from django.contrib import admin
from models import *
class TagsInline(admin.TabularInline):
model = Tag
extra = 1
class EntryAdmin(admin.ModelAdmin):
inlines = (TagsInline, )
admin.site.register(Entry, EntryAdmin)
admin.site.register(Tag)
(Note, this code was written in a browser!)

Resources