Reputation: 1607
Hello I want to make a change to my models.py.
I have two tables. Client and Note. What I want to do is have a note field in the clients table which should have some one-one relationship with the Note table.
However,the note will have to store some user, client and a date time. So I need another table to do this. But I want some one-one relationship with client and note.
A client should only be able to store in one note. A note has a user, datetime, a note text field and client (If I have a note field in the Client's table, I don't think I need this entry).
models.py
class Client(models.Model):
name = models.CharField(max_length = 40)
telephone = models.CharField(max_length = 20)
website = models.URLField(verify_exists = False)
fax = models.CharField(max_length = 20)
email = models.EmailField()
is_active = models.BooleanField()
def __unicode__(self):
return self.name
class Note(models.Model):
client = models.ForeignKey(Client)
datetime = models.DateTimeField(default=datetime.now)
user = models.ForeignKey(User)
note = models.TextField()
def __unicode__(self):
return unicode(self.user)
Upvotes: 0
Views: 227
Reputation: 5362
Justinas is right. You should add a note field to your Client model, which makes the FK on your Note model obsolete. Something like this should get you started:
class Note(models.Model):
datetime = models.DateTimeField(default=datetime.now)
user = models.ForeignKey(User)
note = models.TextField()
def __unicode__(self):
return unicode(self.user)
class Client(models.Model):
name = models.CharField(max_length = 40)
telephone = models.CharField(max_length = 20)
website = models.URLField(verify_exists = False)
fax = models.CharField(max_length = 20)
email = models.EmailField()
is_active = models.BooleanField()
note = models.OneToOneField(Note)
def __unicode__(self):
return self.name
Upvotes: 0
Reputation: 347
I don't quite get the question, but I guess you need the OneToOneField
Upvotes: 1