When i try to create the reviews table, i get the the following error message. The models.py and error message in the terminal is mentioned below.
models.py
class reviews(models.Model):
reviewee = models.ForeignKey('Person', on_delete=models.CASCADE)
reviewer = models.ForeignKey('Person', on_delete=models.CASCADE)
review = models.TextField()
rating = models.FloatField()
class Person(models.Model):
email = models.CharField(max_length=30)
pwd = models.CharField(max_length=30)
type = models.CharField(max_length=30)
terminal output
SystemCheckError: System check identified some issues:
ERRORS:
pfapp.reviews.reviewee: (fields.E304) Reverse accessor for 'reviews.reviewee' clashes with reverse accessor for 'reviews.reviewer'.
HINT: Add or change a related_name argument to the definition for 'reviews.reviewee' or 'reviews.reviewer'.
pfapp.reviews.reviewer: (fields.E304) Reverse accessor for 'reviews.reviewer' clashes with reverse accessor for 'reviews.reviewee'.
HINT: Add or change a related_name argument to the definition for 'reviews.reviewer' or 'reviews.reviewee'.
System check identified 2 issues (0 silenced).
The logic behind my models is that on person can review another person. Also, when either reviewer or reviewee is deleted from the table, the review should also be deleted.
i hope you got my idea.
You can do and then migrate:
reviewee = models.ForeignKey('Person', on_delete=models.CASCADE)
reviewer = models.ForeignKey('Person', on_delete=models.CASCADE, related_name="reviewer", null=True)
Now, on reverse you can get reviewee from person.reviews which is by default. And, get reviewer from person.reviewer. Have a look at this SO question for greater understanding.
Related
I am a newbie to Django, While running the django application using python3 manage.py runserver
Because of the way I have created the model I am getting an error like
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:
ERRORS:
database.LeadsStatus.company_id: (fields.E305) Reverse query name for 'LeadsStatus.company_id' clashes with reverse query name for 'LeadsStatus.companies'.
HINT: Add or change a related_name argument to the definition for 'LeadsStatus.company_id' or 'LeadsStatus.companies'.
database.Users.id: (fields.E007) Primary keys must not have null=True.
HINT: Set null=False on the field, or remove primary_key=True argument.
System check identified 2 issues (0 silenced).
This is how I've created model.
class LeadsStatus(models.Model):
id = models.IntegerField(primary_key=True, auto_created=True)
company_id = models.ForeignKey('Companies', null=False, db_index=True, on_delete=models.CASCADE)
status = models.CharField(max_length=120)
users_id = models.IntegerField()
assign_date = models.DateTimeField()
companies = models.OneToOneField('Companies', on_delete=models.CASCADE)
I believe the error might be because of the way I have created a OneToOneField. How do I solve this error. Please help me understand. Thanks
If you really need 2 foreing keys to the model Companies, then you need to set the option related_name to distinguish them.
You can change your class to, for example, this:
class LeadsStatus(models.Model):
...
company_id = models.ForeignKey(
'Companies',
null=False,
db_index=True,
on_delete=models.CASCADE,
related_name='leadsstatus_1_set')
...
companies = models.OneToOneField(
'Companies',
on_delete=models.CASCADE,
related_name='leadsstatus')
You could also set related_name on just one of these 2 fields, as long as it is different from the default value of leadsstatus_set.
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?
i'm having problems with the concept of relationship in django models.
Let's see my example:
I have this table/class into models.py:
class PacketEventsInformation(models.Model):
ID_IP_Source = models.<Relationship>(Ips, on_delete=models.CASCADE)
ID_IP_Dest = models.<Relationship>(Ips, on_delete=models.CASCADE)
ID_Source_Port = models.<Relationship>(Ports, on_delete=models.CASCADE)
ID_Dest_Port = models.<Relationship>(Ports, on_delete=models.CASCADE)
Protocol = models.CharField(max_length=20)
ID_Source_MAC = models.<Relationship>(Macs, on_delete=models.CASCADE)
ID_Dest_MAC = models.<Relationship>(Macs, on_delete=models.CASCADE)
RAW_Info = models.TextField()
TAG = models.ForeignKey(Tags, on_delete=models.CASCADE)
def __str__(self):
return '%s' % self.id
At this point, I defined the relationship between all my ID fields and ID fields of another tables/classes (Pkey) like ForeignKey.
Well, If I execute migrate into my terminal, I get this:
./manage.py migrate
app.PacketEventsInformation.ID_Dest_MAC: (fields.E304) Reverse accessor for 'PacketEventsInformation.ID_Dest_MAC' clashes with reverse accessor for 'PacketEventsInformation.ID_Source_MAC'.
......
I understand the definition of ManyToMany, OneToOne and OnetoMany (Foreign Key), but I have no idea why can't do it this. Maybe the answer could be create intermediate tables with that Fkeys or put OneToOne relation between that ids....
Thanks for your answers =)
PD:
ID_IP_Source/Dest can be the same
ID_Source/Dest_Port can be the same
ID_Source/Dest_MAC can be the same
In django when you have multiple foreign keys pointing at the same model, you need to use related_name to distinguish them:
ID_IP_Source = models.<Relationship>(Ips, on_delete=models.CASCADE, related_name="id_ip_source")
ID_IP_Dest = models.<Relationship>(Ips, on_delete=models.CASCADE, related_name="id_ip_dest")
I want created a kind social network for my school in django.
My Problem is that I create a follow function and django give me a Error Response.
How can I handle that?
django code:
class User(User):
joined = models.DateTimeField(auto_now_add=True)
is_activated = models.BooleanField(default=False)
def __unicode__(self):
return self.username
class Following_User(models.Model):
user_id = models.ForeignKey(User)
user_follow = models.ForeignKey(User)
and heres the Error Response:
ERRORS:
cherry.Following_User.user_follow: (fields.E304) Reverse accessor for 'Following_User.user_follow' clashes with reverse accessor for 'Following_User.user_id'.
HINT: Add or change a related_name argument to the definition for 'Following_User.user_follow' or 'Following_User.user_id'.
cherry.Following_User.user_id: (fields.E304) Reverse accessor for 'Following_User.user_id' clashes with reverse accessor for 'Following_User.user_follow'.
HINT: Add or change a related_name argument to the definition for 'Following_User.user_id' or 'Following_User.user_follow'.
System check identified 2 issues (0 silenced).
If you are having a model that contains two fields that as foreign key of another model, you need to specify the related_name for each of them.
class Following_User(models.Model):
user_id = models.ForeignKey(User, related_name="following_user_id")
user_follow = models.ForeignKey(User, related_name="following_user_follow")
https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.ForeignKey.related_name
The reason to have related_name is so that django would give you reverse relation if you want to query from the foreign key. For single field that has the relationship with current model, django gives you a default one blahfield_set, but in this case the related_name would be used to remove ambiguity.
I'm currently building a helpdesk ticketing system as a school project. I am using the built in django auth system, and I'd like to refer to user IDs from the auth sytem. For example, a ticket will be assigned to a certain helpdesk employee. A part of my model:
class Ticket(models.Model):
category = models.ManyToManyField(Category)
title = models.CharField(max_length=30)
submitted = models.DateTimeField(editable=False)
submitter = #reference to user
assignedTo = #reference to helpdesk employee
Users are in the group user, helpdesk employees are in the group helpdeskemployee of the django auth system.
I already found this and this
So I tried this:
class Ticket(models.Model):
category = models.ManyToManyField(Category)
title = models.CharField(max_length=30)
submitted = models.DateTimeField(editable=False)
submitter = models.OneToOneField(User)
assignedTo = user = models.OneToOneField(User)
But that gives the folowing error while running python manage.py syncdb:
CommandError: One or more models did not validate:
deskman.ticket: Accessor for field 'submitter' clashes with related field 'User.ticket'. Add a related_name argument to the definition for 'submitter'.
deskman.ticket: Reverse query name for field 'submitter' clashes with related field 'User.ticket'. Add a related_name argument to the definition for 'submitter'.
deskman.ticket: Accessor for field 'assignedTo' clashes with related field 'User.ticket'. Add a related_name argument to the definition for 'assignedTo'.
deskman.ticket: Reverse query name for field 'assignedTo' clashes with related field 'User.ticket'. Add a related_name argument to the definition for 'assignedTo'.
deskman.ticket: Accessor for field 'assignedTo' clashes with related field 'User.ticket'. Add a related_name argument to the definition for 'assignedTo'.
deskman.ticket: Reverse query name for field 'assignedTo' clashes with related field 'User.ticket'. Add a related_name argument to the definition for 'assignedTo'.
Firstly, you probably don't want to use a OneToOneField. That would imply that a user can only ever have one single ticket. A ForeignKey relationship would be better.
class Ticket(models.Model):
category = models.ManyToManyField(Category)
title = models.CharField(max_length=30)
submitted = models.DateTimeField(editable=False)
submitter = models.ForeignKey(User)
assignedTo = models.ForeignKey(User)
The reason you are getting an error is that you have two relationships to the same model from your Ticket. This means if you have a User object and you are trying to reverse it, it's not clear via which relationship you want to use:
user.ticket_set.all()
# Do you want `submitter` tickets, or `assignedTo` tickets? It's not clear
To fix it, add a related_name attribute to each field
class Ticket(models.Model):
category = models.ManyToManyField(Category)
title = models.CharField(max_length=30)
submitted = models.DateTimeField(editable=False)
submitter = models.ForeignKey(User, related_name="tickets_submitter")
assignedTo = models.ForeignKey(User, related_name="tickets_assignedto")
Now you can get both reverse relationship separately:
user.tickets_submitter.all()
user.tickets_assignedto.all()