我有幾個模型,並且對如何創建所有關聯有點困惑。我有個人資料,活動,消息和收件箱。每個配置文件都有一個保存事件的eventList。每條消息也與一個事件相關聯。每個收件箱都與一個配置文件和多條消息相關聯。我想要做的是,無論何時創建一個消息對象,它都會被插入到每個持有消息與其eventList中關聯的事件的用戶的收件箱中。提供了我的模型和我寫的觀點:將消息放入收件箱(Django模型)
class Profile(models.Model):
user = models.OneToOneField(User)
name = models.CharField(max_length=50)
eventList = models.ManyToManyField(Event, blank="TRUE", null="TRUE", related_name='event_set+')
ownedEvent = models.ManyToManyField(Event, blank="TRUE", null="TRUE", related_name='owned_set')
def __unicode__(self):
return self.name
class inbox(models.Model):
def __unicode__(self):
return self.user.name
user = models.OneToOneField(Profile)
message = models.ManyToManyField(message, blank="TRUE", null="TRUE")
read = models.BooleanField(default = 0)
class message(models.Model):
def __unicode__(self):
return unicode(self.body)
def save(self, *args, **kwargs):
if not self.id:
self.created = datetime.datetime.today()
super(message, self).save(*args, **kwargs)
body = models.CharField(max_length=250)
eid = models.ForeignKey(Event)
@login_required
def sendMail(request):
event_id = request.POST['event_id']
e = Event.objects.get(id = event_id)
text = request.POST['body']
m = message(eid = e, body = text)
m.save()
users = e.eventList_set.all()
return HttpResponse(status = 200)
如果你想使用Python/Django的編碼風格保持一致,你可能會考慮:1)資本化的類名(例如'Inbox')2)使用小寫字母,並強調功能和屬性名稱(例如'send_mail','event_list')3)使用M2M字段的複數名稱(例如'messages','owned_events')。不過,這取決於你! –