I have an unusual case. In my models.py, I have an Event element, and two different types of user elements that refer to it as seen below.
class User(models.Model):
attending = models.ManyToManyField('Event', related_name='User_attending')
hosting = models.ForeignKey('Event', related_name='User_hosting')
username = models.CharField(max_length=40)
class Event(models.Model):
#event title
title = models.CharField(max_length=200)
#time created
created = models.DateField('Date Created',auto_now_add=True)
In my views.py, I when I want to create a user, I have some of the following:
user = User()
user.username = ""
user.gender = 0
user.fName = ""
user.lName = ""
....
user.save
This is when my browser yells at me and says:
Integrity Error,
list_user.hosting_id may not be NULL
Any ideas for a way around this without creating an Event element right there?
It appears you want to create a
Userwithout givinghostinga value. You can do this by addingblank=True, null=Trueto the field:You’ll also need to add a migration to fix the database (as the ability to have an empty field needs to be allowed by the database). I suggest using South for this purpose.