I want to have a user select multiple events from another table in django. How do i achieve it?
I am making a Django project. I want that my users table field should automatically update when they register for an event. I Have used ForeignKey
in my Events model to achieve this. But the problem here is, I can select only 1 user in the events model when creating a new event or editing it(since the relationship is manyToOne). What I want is my events model should show every user that has registered for that event (Something like this).
- Am I using the right relationship?
- If Not, what should be the correct relationship that I should use?
I am attaching my models for Users and Events. Kindly Look it through.
accounts/models.py
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(db_index=True, unique=True, max_length=254)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255, null=True, blank=True)
mobile = models.CharField(max_length=50, null=True, blank=True)
address = models.CharField(max_length=200, null=True, blank=True)
short_description = models.CharField(max_length=180, null=True, blank=True)
instagram_url = models.URLField(null=True, blank=True)
linkedin_url = models.URLField(null=True, blank=True)
# is_member = models.BooleanField(default=False)
is_staff = models.BooleanField(default=True)
is_active = models.BooleanField(default=True)
is_superuser = models.BooleanField(default=False)
objects = CustomUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name']
class Meta:
verbose_name = 'User'
verbose_name_plural = 'Users'
models.py
class Event(models.Model):
banner_image = models.ImageField(upload_to="Events/", max_length=400)
event_name = models.CharField(max_length=150, null=False)
organiser_of_event = models.CharField(max_length=200)
format_of_event = models.CharField(max_length=300, null=True, blank=True)
date_of_event = models.DateField(auto_now_add=False)
registration_fees = models.IntegerField(default=0, help_text="Enter Registration Fees For The Event in Rupees")
registration_link = models.URLField(null=True, blank=True)
details = models.TextField(null=True, blank=True)
is_draft = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# user = models.ForeignKey(User, on_delete=models.SET_NULL)
def __str__(self):
return self.event_name