新手在这里。
想知道多个用户如何拥有或管理一个对象实例?
我有一个班级:
class Todo(models.Model):
todolist = models.ForeignKey('TodoList', default=1)
todo = models.TextField(max_length=5000)
order = models.PositiveIntegerField(default=1)
status = models.SlugField(choices=STATUS_CHOICES)
description = models.TextField(max_length=5000, null=True, blank=True)
due_date = models.DateTimeField(blank=True, null=True)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
def __unicode__(self):
return self.todo
class TodoList(models.Model):
title = models.CharField(max_length=120)
order = models.PositiveIntegerField(default=1)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
def __unicode__(self):
return self.title
我应该如何让多个用户使用TodoList实例?
答案 0 :(得分:1)
您需要使用ManyToManyField
:
from django.contrib.auth.models import User
class TodoList(models.Model):
users = models.ManyToManyField(User)