So I'm building a Django application,
and these are a few models I have:
class MagicType(models.Model):
name = models.CharField(max_length=155)
parent = models.ForeignKey('self', null=True, blank=True)
class Spell(models.Model):
name = models.CharField(max_length=250, db_index=True)
magic_words = models.CharField(max_length=250, db_index=True)
magic_types = models.ManyToManyField(MagicType)
When syncing the models I'm getting this error:
AttributeError: 'ManyToManyField' object has no attribute '_get_m2m_column_name'
Is there a reason this is happening? How can I fix this?
Help would be very much appreciated.
EDIT:
I'm using django-evolution http://code.google.com/p/django-evolution/
I suggest you use django-extensions , this will give you a commnad sqldiiff that works better than evolution, because there is a problem creating the intermediate table between MagicType and MagicType.
I suggest you run the command sqlall yourapp and execute directly the sql code of the creation of the new intermediate table. Evolution doesn't it for you :(
Is MagicType declared in the same models file (and before) Spell?
Does magic_types = models.ManyToManyField('MagicType') work (with 'MagicType' quoted)?
Related
I've been building a Django website and included a UUID field "customer_id" in my initial "Customer" model. Finally, I decided to drop it. But when I try to delete it from my models.py, Django throws
SystemCheckError: System check identified some issues:
ERRORS:
<class 'accounts.admin.CustomerAdmin'>: (admin.E035) The value of 'readonly_fields[1]' is not a callable, an attribute of 'CustomerAdmin', or an attribute of 'accounts.Customer'.
Here is the code of models.py
from django.db import models
import uuid
# Create a base model to make sure we keep track of creation and edits
class ModelBaseClass(models.Model):
date_created = models.DateTimeField(auto_now_add=True, null=True)
date_modified = models.DateTimeField(auto_now=True, null=True)
class Meta:
abstract = True
# Create your models here.
class Customer(ModelBaseClass):
customer_id = models.UUIDField(default=uuid.uuid4, #this is the field i try to drop
editable=False,
unique=True)
name = models.CharField(max_length=200, null=True)
email = models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
What I tried so far:
I suspect that this could be related to existing data or some other dependencies. So...
I deleted the sqlite database, deleted all migration files and ran
"python manage.py makemigrations" and "python manage.py migrate".
I ran python manage.py flush.
I also tried to change the editable=False to editable=True and migrate before dropping,
but it didn't change anything.
It's perhaps also worth mentioning that my "Customer" model a relation to another model.
Could someone explain me why Django is preventing me from deleting this field and how to resolve this?
Thanks! :)
Could someone explain me what's going on and how to resolve this?
As the error says, you have a model admin named CustomerAdmin. Indeed:
<class 'accounts.admin.CustomerAdmin'>: (admin.E035) The value of 'readonly_fields[1]' is not a callable, an attribute of 'CustomerAdmin', or an attribute of 'accounts.Customer'.
For the readonly_fields, it lists the customer_id, but since that field is no longer available, it raises the error.
I'm trying to integrate Django-Geoposition package into my Django project. I have my model:
class Post(models.Model):
position = GeopositionField(default= Geoposition(40.00,73.88))
votes= models.IntegerField(default=0)
date= models.DateTimeField(auto_now=False, auto_now_add=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
sound= models.FileField()
I've tried leaving no value for position, but I can't make migrations without a default value. I've also gone ahead and removed all instances of the model from the admin. Even with what I have above (which I found from another person with the same issue) I'm getting a
AttributeError: 'Geoposition' object has no attribute 'split'
when I try to make the migration. Anyone have any pointers?
I used blank=true so that I didn't have to put in a default value
I have 2 app inside a django project. I want to import a model from one app to another. But it gives me
NameError: name 'JobGenre' is not defined
when I try to syncdb
In customer.models
from job.models import JobGenre
class Worker(Costumer):
keyword=models.ForeignKey(JobGenre, null=True)
and in job.models
class JobGenre(models.Model):
genre=models.CharField(max_length=40)
if i use
keyword=models.ForeignKey('job.models.JobGenre', null=True)
it gives
Error: One or more models did not validate:
costumer.worker: 'keyword' has a relation with model job.models.JobGenre, which has either not been installed or is abstract.
What should I do in this situation?
keyword=models.ForeignKey('job.models.JobGenre', null=True)
Looks incorrect to me.
Try instead:
keyword.models.ForeignKey('job.JobGenre', null=True)
You don't need to give the full package path to ForeignKey method. Just giving appname.modelclass will work.
keyword=models.ForeignKey('job.JobGenre', null=True)
should work. please refer here.
I have a class as following:
class Mission(models.Model):
taxi = ForeignKey(Taxi, null=True, blank=True, unique=True, related_name="mission")
passenger = ForeignKey(Passenger, null=True, blank=True, unique=True, related_name="mission")
Now there's a method in the Class Taxi:
def turn_free(self):
....
self.mission_set.clear()
passenger.mission_set.clear() # passenger has been fetched
The first attempt to clear mission in Taxi proceed successfully, but the second one for the passenger reports an error: ccst_mission.passenger_id may not be NULL
Could someone help me?
Make sure your database has been updated properly.
For example, if you originally had (without specifying null=True)
passenger = Foreignkey(Passsenger, unique=True)
And added the null=True later on, a regular syncdb will not update that column to allow null.
If any of that sounds familiar, try doing an sqlclear on the app (or deleting the database entirely) then doing a fresh syncdb.
I have a django app which basically is just a photo album. Right now I have two models: Image and Album. Among other things, each Album has a foreign key to an Image to be its thumbnail and each Image has a foreign key to the Album it belongs in. However, when I try to use manage.py syncdb or manage.py sqlall I get errors saying the class not defined first in models.py isn't defined when it is used in the first class defined.
models.py (abridged):
from django.db import models
import os
class Album(models.Model):
thumb = models.ForeignKey(Image, null=True, blank=True)
class Image(models.Model):
image = models.ImageField(upload_to='t_pics/images')
thumb = models.ImageField(upload_to='t_pics/images/thumbs')
album = models.ForeignKey(Album)
Error I get when I do manage.py sqlall appname:
[...]
File "/path/to/file/appname/models.py", line 4, in ?
class Album(models.Model):
File "/path/to/file/appname/models.py", line 5, in Album
thumb = models.ForeignKey(Image, null=True, blank=True)
NameError: name 'Image' is not defined
I get the same error when I switch the order of the classes in models.py except it says 'Album' undefined instead of 'Image' undefined I also tried commenting the dependancy in the first class then uncommenting after everything else was successfully imported but that didn't help. How should I go about making this work? I'm reluctant to make an entire third class Thumb because it will have a lot of the same code as Image I'm also pretty sure I could manually add the foreign key to the database but I want this to be clean and not hackish.
You don't actually have a circular reference; the issue is that, at the time you define Album, you haven't defined Image yet. You can fix that by using a string instead:
class Album(models.model):
thumb = models.ForeignKey('Image', null=True, blank=True)
However, in this case, you might want to use a OneToOneField instead of a foreign key. (Note that you'll still have to use the trick with the string, though).
Use quotes to force a lazy reference:
models.ForeignKey('Image', null=True, blank=True)
Also, ForeignKey.related_name is your friend (avoids back-reference name clashes).
This is old but anyway, i'd like to say that I don't see a reason for attribute album in model Image. In my opinion, it is not really needed.