'Django Pass Extra Information To ManyToMany Model Signal
i have a manytomany django related models which needs an entry created automatically when one model is created using signals. my challenge is passing extra information to the signal for the intermediate Team model table?
class Listing(models.Model):
"""
The Listing Model
"""
# model fields --> [name, description, email, phone]
team = models.ManyToManyField(User, through = 'Team')
class Team(models.Model):
"""
A Group Of Users To Manage A Listing
"""
AUTHOR = 'Author'
EDITOR = 'Editor'
ROLE_CHOICES = [(AUTHOR, 'Author'), (EDITOR, 'Editor')]
listing = models.ForeignKey(Listing, on_delete = models.RESTRICT, related_name = 'businesses')
author = models.ForeignKey(User, on_delete = models.RESTRICT, related_name = 'buddies')
role = models.CharField(max_length = 10, choices = ROLE_CHOICES, default = EDITOR)
@receiver(post_save, sender = Listing)
def create_team(sender, instance, created, **kwargs):
if created:
Team.objects.create(listing = instance, author = instance.author, role = 'Author') # <-- How To Pass User Instance To Signal?!
How do i pass the user instance to the signal to create author for the listing?
for example Listing.objects.create(..., author = user) to create the Listing and it's Author(User)?
Edit
A Listing is created by a User, who is assigned the role of 'Author' and is part of the Team to manage the Listing. so whenever i call Listing.objects.create(), how can i pass the user to the model to create the Team as well using the signal create_team, else the listing will be without a User i.e Author
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
