Reputation: 1588
I tried to add a ManyToManyField to user using a OneToOneField relation first. Here is my code:
ENTERPRISE_TYPE_CHOICES = (
('a', "A"),
('b', "B"),
('c', "C"),
)
class Enterprise(models.Model):
et_type = models.CharField(
max_length = 10,
choices=ENTERPRISE_TYPE_CHOICES,
default='a'
)
name = models.TextField(max_length=100)
number = models.TextField(max_length=7)
def __str__(self):
return "%s %s %s" % (self.et_type, self.name, self.number)
class Secretaire(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
workshop = models.ManyToManyField(Enterprise)
@receiver(post_save, sender=User)
def create_user_secretaire(sender, instance, created, **kwargs):
if created:
Secretaire.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_secretaire(sender, instance, **kwargs):
instance.profile.save()
Then in the python manage.py shell I try :
from django.contrib.auth.models import User
u = User.objects.create(username="a", email="[email protected]", password="a")
And it throw the error :
ProgrammingError: relation "helpdesk_secretaire" does not exist LINE 1: INSERT INTO "helpdesk_secretaire" ("user_id") VALUES (11) RE...
(The User is still created but no can't acces to user.secretaire, it throws the same error)
Do you have any clue about why I get this error?
Upvotes: 0
Views: 62
Reputation: 2163
As per the comments on the original question: You need to run makemigrations
and migrate
first. The error message you are getting indicates that the table does not exist in the database.
Upvotes: 1