Reason for IntegrityError on Field deletion? - python

I have a webservice setup with Django backend and I am trying to delete entries for my Field objects. Each Field is assigned to a User and there are a number of Assessments done in the Field. The Assessments are linked to the Fields via a foreign key. Upon deletion of the Field object I want to also delete all the Assessments for that Field, but keep the User.
I played around with the on_deletion parameter and if I set it to CASCADE the Django admin page shows me all the associated Assessment objects if I try to delete the Field object. However, I am still getting the following error:
IntegrityError at /admin/dfto/field/ update or delete on table "field"
violates foreign key constraint "assessment_field_uuid_fkey" on table
"assessment" DETAIL: Key
(uuid)=(f3a52c10-33be-42f9-995d-482025cea17b) is still referenced from
table "assessment".
These are my models for Reference:
class Assessment(models.Model):
uuid = models.TextField(primary_key=True)
longitude = models.FloatField(blank=True, null=True)
latitude = models.FloatField(blank=True, null=True)
field_uuid = models.ForeignKey('Field', models.CASCADE, db_column='field_uuid',blank=True, null=True, related_name='assessments')
class Meta:
db_table = 'assessment'
class Field(models.Model):
uuid = models.TextField(primary_key=True)
name = models.CharField(max_length=50, blank=True, null=True)
country = models.CharField(max_length=50, blank=True, null=True)
user_email = models.ForeignKey('User', models.DO_NOTHING, db_column='user_email')
crop_uuid = models.ForeignKey(Crop, models.CASCADE, db_column='crop_uuid')
class Meta:
db_table = 'field'
Can someone explain to me why I am getting this error and/or provide a fix for me?

I think it's because field_uuid have argument 'models.CASCADE' so when you are deleting it, django also tries to delete any refences to this particular key, don't know how to fix it tho.

Related

Having trouble wrapping my head around follower/target models in Models.py

I have just started with making a similar site to Pinterest and the site has follower/target system that I have barely any understanding of. So far, my models.py code is below:
from django.db import models
class User(models.Model):
username = models.CharField(max_length=45, null=True)
email = models.CharField(max_length=200, null=True)
password = models.CharField(max_length=200)
nickname = models.CharField(max_length=45, null=True)
target = models.ManyToManyField(self, through='Follow')
follower = models.ManyToManyField(self, through='Follow')
class Meta:
db_table = 'users'
class Follow(models.Model):
follower = models.ForeignKey(User, on_delete=models.CASCADE, related_name='targets')
target = models.ForeignKey(User, on_delete=models.CASCADE, related_name='followers')
class Meta:
db_table = 'follows'
This code was made with reference to another StackOverflow thread
Django models: database design for user and follower
However, I am having trouble understanding how using "related_name='targets' in 'follower' and "related_name='followers'" in 'target' where I can't see any 'targets'(plural) or 'followers'(plural) in other areas of models.py
Should I get rid of that related_name, since there is no such table called "followers" or "targets"? And if you spot major errors in my code or logic, can you tell me? Thanks!
Should I get rid of that related_name, since there is no such table called followers or targets.
There is never a table named followers or targets. The related_name [Django-doc] is a conceptual relation Django makes to the other model (in this case User). It means that for a User object myuser, you can access the Follow objects that refer to that user through target for example with myuser.followers.all(), so:
Follow.objects.filter(target=myuser)
is equivalent to:
myuser.followers.all()
The default of a related_name is modelname_set, so here that would be follow_set. But if you remove both related_names, then that would result in a name conflict, since one can not add two relations follow_set to the User model (and each having a different semantical value).
if you spot major errors in my code or logic, can you tell me?
The problem is that since ManyToManyFields refer to 'self' (it should be 'self' as string literal), it is ambigous what the "source" and what the target will be, furthermore Django will assume that the relation is symmetrical [Django-doc], which is not the case. You should specify what the source and target foreign keys are, you can do that with the through_fields=… parameter [Django-doc]. It furthermore is better to simply define the related_name of the ManyToManyField in reverse, to avoid duplicated logic.
from django.db import models
class User(models.Model):
username = models.CharField(max_length=45, unique=True)
email = models.CharField(max_length=200)
password = models.CharField(max_length=200)
nickname = models.CharField(max_length=45)
follows = models.ManyToManyField(
'self',
through='Follow',
symmetrical=False,
related_name='followed_by',
through_fields=('follower', 'target')
)
class Meta:
db_table = 'users'
class Follow(models.Model):
follower = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name='targets'
)
target = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name='followers'
)
class Meta:
db_table = 'follows'
Here a User object myuser can thus access myuser.follows.all() to access all the users that they follow, myuser.followed_by.all() is the set of Users that follow myuser. myuser.targets.all() is the set of Follow objects that he is following, and myuser.followers.all() is the set of Follow objects that are following that user.

How to Model Peer to Peer Transactions in Django?

I'm trying to figure out the correct way to model transactions between users in an ecommerce site I'm building. I want the transaction to be available to both users in a dashboard.
It seems incorrect to have the transaction, and all its details, saved to both users objects. I think I need to create a separate model for transactions, with a unique id for each transaction. Then, in each user's object I could simply save that transaction id.
Would I then simply give each transaction two primary keys, one for the purchaser's user.id and one for the purchasee's user.id? Is this "many to many"?
Thanks for any guidance here.
Edit - Here is what I've tried:
class Transaction(models.Model):
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
purchaser = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
create_time = models.DateField(auto_now=True)
item = models.ForeignKey(Item, on_delete=models.CASCADE)
However, this gives an error items.Transaction.owner: (fields.E304) Reverse accessor for 'Transaction.owner' clashes with reverse accessor for 'Transaction.purchaser'.
HINT: Add or change a related_name argument to the definition for 'Transaction.owner' or 'Transaction.purchaser'.
You should add related_name parameter for both fields each has its own name, Like :
class Transaction(models.Model):
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name = 'transaction_owner')
purchaser = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name = 'transaction_purchaser')
create_time = models.DateField(auto_now=True)
item = models.ForeignKey(Item, on_delete=models.CASCADE)
More Context : Django: Why do some model fields clash with each other?

How to access data using reverse foreign key reference in django

I have a model named UserProfile and a model PersonalInformation. I would like to fetch all the data of PersonalInformation using UserProfile model when the user is logged into the webiste but i have a foreign key refernce in the PersonalInformation model with the UserProfile model so how do i fetch the personal information using UserProfile model?
User Profile Model :
class UserProfile(models.Model):
"""Represents a user's model inside our system"""
email = models.EmailField(max_length=255, unique=True)
name = models.CharField(max_length=255)
profile_picture = models.ImageField(upload_to='photos/%y/%m/%d/')
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
highest_degree_earned = models.CharField(max_length=255, blank=False)
college_name = models.CharField(max_length=255, blank=False)
graduation_year = models.IntegerField(default=2020, blank=False)
Personal Information Model :
class PersonalInformation(models.Model):
"""Represents a user's personal Infromation inside our system"""
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
mobile = models.CharField(max_length=10 ,blank=True)
bio = models.TextField(max_length=200, blank=True)
college_university = models.CharField(max_length=100, blank=False)
course = models.CharField(max_length=100, blank=False)
First of all, in the code, you are showing you have the names of the models wrong. The UserProfile model name is set as PersonalInformation, change it or the migrations won't work (it's not accepted on the database no matter which one you're using).
Referent to the question you're asking, to fetch the related instance of PersonalInformation of a certain UserProfile instance you should just query the next:
user = UserProfile.objects.get(id='') #Introduce the id of the user you want to fetch its personal information.
user.personalinformation_set.all() # This will return you a QuerySet with all the related instances of PersonalInformation class.
user.personalinformation_set.get(id='') #To get a specific one or you may use a filter to get a filtered QS.
If you want, you can use the related_name attribute for ForeignKey class in order to set a different name from personalinformation_set.
I recommend you too to read the Django documentation, it's really well explained and clear I think:
https://docs.djangoproject.com/en/2.2/topics/db/examples/many_to_one/
As I've seen in a comment, you may also think to use a OneToOne relation instead of ForeignKey if you only expect one instance of PersonalInformation per User. The documentation is at:
https://docs.djangoproject.com/en/2.2/topics/db/examples/one_to_one/

Django migrations not detecting unique=True change

I am getting the following error after trying to add a foreignkey from CrackingJob.hash_mode_numeric to HashMappings.
Initially i was trying to set the FK directly to HashMappings.hash_mode_numeric without the unique constraint and it rightly gave the error, but after adding unique=True i still get the error. Even when i try to just use the PK (auto generated unique id) as FK, like in the code below.
django.db.utils.ProgrammingError: there is no unique constraint
matching given keys for referenced table "appname_hashmappings"
Relevant code:
class HashMappings(models.Model):
hash_name = models.CharField(max_length=255, unique=True)
hash_mode_numeric = models.IntegerField(unique=True)
example_hash = models.TextField(max_length=2500)
supported = models.BooleanField(default=0)
class Meta:
ordering = ['hash_name']
def __str__(self):
return f'{self.hash_name}'
class CrackingJob(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL)
description = models.CharField(max_length=255)
hash_mode_numeric = models.ForeignKey(HashMappings, on_delete=models.CASCADE)
try to clear data in hashmappings table and then run migrate command -> python manage.py migrate

customize django admin object detail page url(object change url)

I am trying to override the default django admin change url.
In my table i have composite primary key.
class ABC(models.Model):
code = models.ForeignKey('PQR', models.DO_NOTHING, db_column='code', primary_key=True)
language = models.ForeignKey(Languages, models.DO_NOTHING, db_column='language')
me_name = models.TextField()
common_name = models.TextField(blank=True, null=True)
abbreviation = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'abc'
unique_together = (('code', 'language'), ('language', 'me_name'),)
Now my admin url in django admin for each object is /admin/home/abc/{{code}}/change/.
But i have repeated codes in objects because primary key is composite of ('code', 'language'). So for objects which have repeated code are throwing
Error
MultipleObjectsReturned at /admin/home/abc/X00124/change/
get() returned more than one ABC -- it returned 2!
here X00124 this code associated with more than one object.
What i want here that override the modeladmins get_urls() method and construct the url /admin/home/abc/{{code}}/{{language}}/change/.
I tried but no success. Help will be appreciated.
I made a virtual key, that represents compound key as a single value, that allows to use Django admin without code changes -
https://viewflow.medium.com/the-django-compositeforeignkey-field-get-access-to-a-legacy-database-without-altering-db-tables-74abc9868026

Categories

Resources