Django error when removing ForeignKey relationship - python

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.

Related

Why leads deletion of UUIDField to Django SystemCheckError

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.

Delete 2 model different objects which reference each other as foreign keys

We have two Django models:
class Project(models.Model):
project_title = models.CharField(max_length=30)
owner = models.ForeignKey(User, null=True, on_delete=models.DO_NOTHING)
class User(models.Model):
usernmae = models.CharField(max_length=50)
active_project = models.ForeignKey(User, null=True, on_delete=models.DO_NOTHING, related_name='current_project')
I have a user with object (with id say 692). And this user created a project with id=12345, therefore these owner field will get have this particular referenced.
I want to delete that user. But it shows error that
delete on table "app_user" violates foreign key constraint
This is expected as on_delete=models.DO_NOTHING, was set. One way I found out was using on_delete=models.CASCADE.
Question: How should I go about deleting the user (692) without changing the model definition(having to re-run migration)?
Doing it manually by deleting the project first, leads to the same foreign-key error, as owner field is User object.
How to handle this mutual foreign key relationship while deleting, as deleting any one of those two throws the foreign-key exception?
Update
Some correction in the model definition username is the field name instead of usernmae (typo). And the foreignkey for project is Project not the User model.
class Project(models.Model):
project_title = models.CharField(max_length=30)
owner = models.ForeignKey(User, null=True, on_delete=models.DO_NOTHING)
class User(models.Model):
username = models.CharField(max_length=50)
active_project = models.ForeignKey(Project, null=True, on_delete=models.DO_NOTHING, related_name='current_project')
IF you really don't want to make a migration (any specific reason?) and if you are ok with doing this manually this time. Then you have two options:
Go into the admin panel and manually change the User field in the project instance to a different user or to NULL. Now you should be able to delete the User instance since it's not referred anymore into the project.
If that worked, you can then delete the project instane as well.
Curios if this will work, let me know!

Why is Django (1.8.3) migrations dropping fields in migration when they're still in the model

I went to add a text field to a model in my django app, made, applied my migrations then all of a sudden, I have tests failing left and right. Turns out, the migration decided to drop a field that still exists in my model.
Before going on, some of the relevant code. First, the effected model:
class CandidateProfile(models.Model):
user_profile = models.ForeignKey(UserProfile, related_name="candidate_profile", null=True, blank=True)
facebook_url = models.CharField(max_length=200, default="")
website = models.CharField(max_length=200, default="")
primary_email = models.CharField(max_length=100, default="")
party = models.ForeignKey(PoliticalParty, related_name="candidate_party", null=True, blank=True)
uploaded_picture = models.CharField(max_length=200, default="")
ref_id = models.CharField(db_index=True, max_length=50, default="")
create_date = models.DateTimeField(auto_now_add=True)
update_date = models.DateTimeField(auto_now=True)
The field I added was facebook_url. Prior to this migration, app working, tests passing, etc. The migration that was generated is:
class Migration(migrations.Migration):
dependencies = [
('users', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='candidateprofile',
name='party',
),
migrations.AddField(
model_name='candidateprofile',
name='facebook_url',
field=models.CharField(default=b'', max_length=200),
),
]
The fix itself is simple enough, I can roll back my migration, and manually remove the migrations.RemoveField. Unit tests did what they were supposed to do in letting me know I screwed something up, but I still get worried about something like this getting through to production, losing data and having to restore from a backup.
My question is: why does Django think that the field should be removed even though, it's clearly present as a foreign key in the model?
Edit: After the field was deleted, I tried changing properties on the party attribute to see if whatever diff-ing mechanism Django uses will pick it up. No dice, changing related_name, null, or blank didn't do anything. Running makemigrations detected no changes.
Also, foreign key model for reference:
class PoliticalParty(models.Model):
name = models.CharField(max_length="100")
full_name = models.CharField(max_length="255")
abbreviation = models.CharField(max_length="20")
ref_id = models.CharField(max_length="50", default="", db_index=True)
create_date = models.DateTimeField(auto_now_add=True)
update_date = models.DateTimeField(auto_now=True)
My guess is that you 'shadowed' the field, by creating an attribute or method with the same name.
class CandidateProfile(models.Model):
party = models.ForeignKey(PoliticalParty, related_name="candidate_party", null=True, blank=True)
def party(self):
"""This method will replace the model field"""
return ''
If you didn't do this, then please try to provide instructions that can recreate the issue (preferably with the latest point release 1.8.7). Dropping an existing field is a very serious data loss issue, and the Django developers would take it very seriously.

Using model inheritance and encounting by non-nullable field error

I used inheritance model in my project after changing the model; but I give non-nullable field error. What should I do?
I am using Django 1.7
class Questions(models.Model):
question_category = models.ForeignKey(Course, blank=False)
question_author = models.ForeignKey(Author, blank=False)
question_details = models.CharField(max_length=100, blank=False, default='')
timestamp = models.DateTimeField(auto_now_add=True)
class TypeFive(Questions):
question_title = models.CharField(max_length=100, blank=False, default=generator(5), unique=True, editable=False)
def __str__(self):
return "{}".format(self.question_title)
class TypeFiveChoice(models.Model):
question_choice = models.ForeignKey(TypeFive)
is_it_question = models.BooleanField(default=False)
word = models.CharField(default='', blank=False, max_length=20)
translate = models.CharField(default='', blank=False, max_length=20)
timestamp = models.DateTimeField(auto_now_add=True)
def __str__(self):
return "{} : {}, {}".format(self.question_choice, self.word, self.translate)
After migrations:
You are trying to add a non-nullable field 'questions_ptr' to typefive without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
In order to inherit from Questions in TypeFive, Django needs to add a relation from TypeFive to Questions. For all records in TypeFive that might already be in the database.
Django now doesn't know which question it should relate TopFive to. This is what the migrate command asks you for. You have a few options, but they greatly depend on your use case and whether you are in early development or if there is a production database where this migration has to run later.
I'm in early development and running it on localhost, so iI don't care
about my records. Now, what should I do?
In this case you haven't much to worry about, when migrate asks you type 1 and then press enter. Now add a primary key of a Questions instance that is in your database and then hit enter again.
Django now relates all TypeFive instances that are currently in the database to this question, so you might have to clean that up afterwards (e.g. by editing the TypeFive in Django admin).
#Nick Brady pointed this out in the question above so I don't mean to take credit but I wanted to highlight.
If your new inheritance class is only used for the purpose of being inherited from, you can easily get around this by setting your parent class to abstract.
class Parent(models.model):
Name = models.CharField(max_length=50)
class Meta:
abstract = True
class Child(Parent):
foobar = models.CharField(max_length=50)
class Meta:
db_table = "typenex_taxonomy_nodes"

Django 1.6: One to Many relationship issue with south

In need of some dire help with setting up relationships in my django model and views.
Just wanted to say thank you! before anyone takes a deep dive below!
Working on an application were I have a One to Many relationship where I have many Products and a few particular products will be related to only one Website.
One of the biggest problems am experiencing is when I try to add a foreign key to my Website Model I get this error:
? The field 'Product.website' does not have a default specified, yet is NOT NULL.
? Since you are adding this field, you MUST specify a default
? value to use for existing rows. Would you like to:
? 1. Quit now, and add a default to the field in models.py
? 2. Specify a one-off value to use for existing columns now
I tried to use this solution here:
Django South - Create Not Null ForeignKey
But to no avail, didnt know what to do after Step #4 and I just got lost regardless.
Models.py in my product_extend app
Product Model:
class Product(models.Model):
"""
The product structure for the application, the products we scrap from sites will model this and save directly into the tables.
"""
product_name = models.CharField(max_length=254, verbose_name=_('Name'), null=True, blank=True)
product_price = CurrencyField( verbose_name=_('Unit price') )
product_slug_url = models.URLField(max_length=200, null=True, blank=True)
product_category = models.CharField(max_length=254, blank=True, null=True)
product_img = models.ImageField('Product Image', upload_to='product_images', null=True, blank=True)
product_website_url = models.URLField(max_length=200, null=True, blank=True)
product_website_name = models.CharField(max_length=254, blank=True, null=True)
#For Admin Purposes, to keep track of new and old items in the database by administrative users
date_added = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name=_('Date added'))
last_modified = models.DateTimeField(auto_now=True, null=True, blank=True, verbose_name=_('Last modified') )
#For Admin Purposes, to make sure an item is active by administrative users
active = models.BooleanField(default=True, verbose_name=_('Active') )
# Foreign Key
website = models.ForeignKey(Website, null=True, related_name='website_to_product')
Website Model
class Website(models.Model):
name = models.CharField(max_length=254, blank=True, null=True, unique=True)
description = models.TextField(null=True, blank=True)
website_slug = models.SlugField(verbose_name=_('Website Slug'), unique=True)
site_logo = models.ImageField('Websites Logo', upload_to='website_logo_images', null=True, blank=True)
menswear = models.BooleanField(default=False, verbose_name=_('Menswear'))
womenswear = models.BooleanField(default=False, verbose_name=_('Womenswear'))
active = models.BooleanField(default=True, verbose_name=_('Active'))
Edit
I shortened this question in an effort to make it more comprehensible and split the second part as another question:
Django 1.6: Displaying a particular models Objects in another template
Is it right to say that when you do the migration, both Product and Websites already exist in models.py? If that is the case, a workaround would be to enter a random value for this
? 2. Specify a one-off value to use for existing columns now
Or you may for the time being set the null to be true. Then after you created the foreign key and added the key values for the existing rows. You can re-set it to be false. Then run the migration process as in https://stackoverflow.com/a/22617012/2774853:
Step 6. Run ./manage.py schemamigration <app> --auto
Step 7. Run ./manage.py migrate <app>
Hope it helps.

Categories