社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  问与答

Django怎么在一个表中写一个一对多的外键包含另一个包含外键的表

秒跑 • 8 年前 • 2731 次点击  

我的需求是这样的,在一个表OneCategory中用models.ManyToManyField('Post')

class Post(models.Model):
    ....
    tag = models.ManyToManyField('Tag')

class OneCategory(models.Model):
    name = models.CharField('一级分类')

我就是想实现这样的功能。

模型大意: 表A中有外键链接到表B,表B中包含其他的外键,表C、D、E 等不包含外键。

我写了这样的模型,老是出现错误,有什么要注意的,我是新手大家看看我的代码错在哪里。

我的 models.py 代码

from django.db import models

class Post(models.Model):
    title = models.CharField('标题', max_length=100)
    content = models.TextField('内容')
    tag = models.ManyToManyField('Tag',blank=True)
    category = models.ForeignKey('Category',blank=True)
    comment = models.ManyToManyField('Comment',blank=True)
    pub_date = models.DateTimeField('发布日期', auto_now_add=True)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = '文章'
        verbose_name_plural = '文章'

class Category(models.Model):
    name = models.CharField('名称', max_length=30)
    des = models.CharField('描述', max_length=300)
    pub_date = models.DateTimeField('创建日期', auto_now_add=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '分类'
        verbose_name_plural = '分类'

class Tag(models.Model):
    name = models.CharField('名称', max_length=20)
    pub_date = models.DateTimeField('创建日期', auto_now_add=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '标签'
        verbose_name_plural = '标签'

class OneCategory(models.Model):
    name = models.CharField('一级分类', max_length=25)
    des = models.CharField('描述', max_length=300)
    pub_date = models.DateTimeField('创建日期', auto_now_add=True, null=True)
   post = models.ManyToManyField('Post',blank=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '主分类'
        verbose_name_plural = '主分类'

class Comment(models.Model):
    nickname = models.CharField('昵称'


    
, max_length=20)
    email = models.EmailField('邮箱', max_length=50)
    website = models.URLField('站点', blank=True)
    content = models.TextField('内容', max_length=1500)
    pub_date = models.DateTimeField('发布日期', auto_now_add=True, null=True)

    def __str__(self):
        return self.nickname

    class Meta:
        verbose_name = '评论'
        verbose_name_plural= '评论'
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/1603
 
2731 次点击  
文章 [ 1 ]  |  最新文章 8 年前
saulshao
Reply   •   1 楼
saulshao    8 年前

下面是我的代码:

多对多的类:

class Employeeship(models.Model):
user = models.ForeignKey(
                            User, 
                            on_delete=models.PROTECT, 
                            null = True,
                        )
department = models.ForeignKey(
                                Department, 
                                on_delete=models.PROTECT, 
                                null = True,
                              )

def __str__ (self):
    return self.name

class Meta:
    db_table = u'employeeship'

部门引用上面的多对多类:

class Department(Common, Rowtracking, Recurrence):
company = models.ForeignKey(
                            Company, 
                            on_delete=models.PROTECT, 
                            null = True,
                            )

#Link department to user                            
employees = models.ManyToManyField(
                                    User,
                                    through='Employeeship',
                                    through_fields=('department', 'user'),
                                  )

def __str__ (self):
    return self.name

class Meta:
    db_table = u'department'