How to Model Peer to Peer Transactions in Django? - python

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?

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.

Django Models Relationship Confusions

I have the following models:
class UserPost(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
class User(AbstractUser):
MALE = 'M'
FEMALE = 'F'
GENDER_CHOICES = (
(MALE, 'Male'),
(FEMALE, 'Female')
)
posts = models.ManyToManyField(Post, through='UserPost')
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Post(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
content = models.TextField()
status = models.CharField(max_length=100)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
When I run python manage.py makemigrations, it raises the following error:
users.User.posts: (fields.E303) Reverse query name for 'User.posts' clashes with field name 'Post.user'.
HINT: Rename field 'Post.user', or add/change a related_name argument to the definition for field 'User.posts'.
There is a many-to-many relationship between User and Post models. Each user can like many posts and each post can be liked by many users.
There is also a many-to-one relationship between User and Post models. Each user can write many posts and each post can be written by only one user.
Shouldn't reverse query name for 'User.posts' be user_set by default. If so, why is this name clashing with field name 'Post.user'? Can someone explain the meaning of this error? Thanks.
Do you need the UserPost model? It looks to have all the same fields as Post, and if you're after efficient querying, Django automatically creates database indexes on foreign keys. Here's a simple setup that should work pretty well:
class User(AbstractUser):
# Your fields go here, but you might not need the posts field
class Post(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='posts')
This would let you do a user.posts.all() to get all of the Post instances that belong to that user.

Access to fields of on reverse relation in django

I have these models:
class Task(models.Model):
user = models.ForeignKey(User)
name = models.CharField()
class Report(models.Model):
task = models.ForeignKey(
Task, blank=True, null=True, related_name='+')
status = models.CharField(max_length=32, choices=Status.CHOICES, default=Status.INCOMPLETE)
Now I want to get all Tasks and their related status.
How do I do this?
At first, '+' is not a valid related_name. It is also not very explicit.
Try replacing '+' with 'reports' instead:
# ...
class Report(models.Model):
task = models.ForeignKey(
Task,
blank=True,
null=True,
related_name='reports' # <<<
)
status = models.CharField(
max_length=32,
choices=Status.CHOICES,
default=Status.INCOMPLETE
)
Then to get all the Tasks with their related status, you can use values:
>>> Task.objects.values('name', 'report__status')
<QuerySet [{'name': 'test', 'report__status': 'OK'}, ...]>
The sign + is exactly what you don't want to have as related_name in this case. It tells Django to not create a backwards relation. Check out here. Chose another valid name or skip this argument, in which case Django will create a reverse relation by default using for the name of the backwards relation the model name lowercased and the suffix _set (see here for details).
However, in your example if you want to get all reports which are related to a task and the corresponding status you do not necessarily need backwards relationships. Try this:
reports = Report.objects.exclude(task__isnull=True).values('task__name', 'status')
Change the value of related_name to something else, like 'reports' for example:
class Report(models.Model):
task = models.ForeignKey(
Task, blank=True, null=True, related_name='reports')
status = models.CharField(max_length=32, choices=Status.CHOICES, default=Status.INCOMPLETE)
Now if you have a Task object (not queryset), you can get a queryset of it's reports using:
reports = task.reports.all()
You can use filter() on the reports if you need to.
reports = task.reports.filter(status='something')

Django Model: Multiple foreign keys or one to one field

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")

Django models: user ID

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()

Categories

Resources