Django模型关系(如何改变关系)

时间:2017-05-29 12:52:13

标签: python django models

我是Django新手。我有一个如下的模型列表。在这个例子中,我有朋友列表和有用户的friendrequestlists,但我想让用户有朋友列表和friendrequestlists。我该如何修改这段代码:

from django.db import models
from datetime import datetime
from django.contrib.auth.models import User, Group


class Word(models.Model):
    definition = models.CharField(max_length=350)
    #add_date = models.DateTimeField(default=str(datetime.now()), editable=False)
    turkish = models.CharField(max_length=50)
    english = models.CharField(max_length=50)
    users = models.ManyToManyField(User)
    groups = models.ManyToManyField(Group)
    creator = models.CharField(max_length=50)
    visibility = models.CharField(max_length=2, default='2')  #0(only me), 1(friends), 2(groups), 3(everbody)

    def __str__(self):
        return self.english

    #def add_date_pretty(self):
    #    return self.add_date
    def summary(self):
        return self.definition[:50] + "..."


class GroupEx(models.Model):
    group = models.OneToOneField(Group)
    admins = models.ManyToManyField(User)

    def __str__(self):
        return self.group.name

class GroupReqList(models.Model):
    group = models.OneToOneField(Group)
    member_request = models.ManyToManyField(User)

    def __str__(self):
        return self.group.name

class UserEx(models.Model):
    user = models.OneToOneField(User)
    casel = models.CharField(max_length=12)

    def __str__(self):
        return self.user.username

class FriendList(models.Model):
    user = models.OneToOneField(UserEx)
    friends = models.ManyToManyField(User)

    def __str__(self):
        return self.user.user.username

class FriendReqList(models.Model):
    user = models.OneToOneField(UserEx)
    friend_requests = models.ManyToManyField(User)

    def __str__(self):
        return self.user.user.username

0 个答案:

没有答案