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.
Related
I'm building a simple recipe app, and so far I have two models: Ingredient and Recipe.
Each recipe should have multiple ingredients, so I laid out my model like this:
class Ingredient(models.Model):
name = models.CharField(max_length=50)
class Recipe(models.Model):
title = models.CharField(max_length=100)
ingredients = models.ForeignKey(Ingredient, on_delete=CASCADE)
instructions = JSONField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=SET_DEFAULT, default='Chef Anon')
When I makemigrations, I get nothing, but when I migrate, I get this ValueError:
ValueError: Cannot alter field cookbook.Recipe.ingredients into cookbook.Recipe.ingredients - they do not properly define db_type (are you using a badly-written custom field?)
Following the example here (Django: Add foreign key in same model but different class), I've tried setting ingredients=models.ForeignKey(Ingredient, on_delete=CASCADE) as well as using lazy syntax ingredients=models.ForeignKey("Ingredient", on_delete=CASCADE), but each time, makemigrations shows no changes, and migrate gives me the same ValueError.
Edit
My imports:
from django.db.models.deletion import CASCADE, SET_DEFAULT, SET_NULL
from django.db.models.fields.json import JSONField
from django.utils import timezone
from django.contrib.auth.models import User```
Try replacing on_delete=CASCADE with on_delete=models.CASCADE
If you have not imported CASCADE separately from models.
All though, in that case you should get a warning that "CASCADE is not defined".
I believe I found the problem: My models.py file was in the root directory, not in the app directory.
I am changing my register model so that there is a foreign key referencing a location. What I want to achieve, is to have a relationship where the Register model can have 0 to many locations.
Originally I set a manytomany field which I realised was a mistake as it gives each Register all of the locations in existence. I just want a subset for each Register.
My model now looks like:
class Register(AbstractBaseUser, models.Model):
username = models.CharField(max_length=20,default='',blank=True)
password = models.CharField(max_length=80,default='',blank=True)
email = models.CharField(max_length=255,default='',blank=True)
#Added 2nd May
#locations = models.ManyToManyField(Location)
#3rd May change to foreign key
locations = models.ForeignKey(Location,on_delete=models.CASCADE, blank=True, null=True, default='')
USERNAME_FIELD = 'username'
The model referenced is:
class Location(models.Model):
locationname = models.CharField(max_length=80,default='',blank=True)
address = models.ForeignKey(Address, on_delete=models.CASCADE)
geolocation = models.ForeignKey(GeoLocation, on_delete=models.CASCADE, default='')
When I try to migrate I get the error below. I have ran makemigrations and if I run it again it states there are no changes.
"NOT NULL constraint failed: register_register.locations_id"
I have been searching other posts and it suggested adding the null=True argument which I have added, but I still get this error. I can't find any posts where this has been done and it still gives this error.
Purging the data from the database using manage.py flush allowed me to re-migrate the projects.
I realised that I had made a mistake with the relationship and had the Foreign key on the wrong table, it needed to be on location so that was fundamentally the issue.
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 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.
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)?