简体   繁体   English

关于多对多的基本 django model 问题

[英]Basic django model question on ManyToMany

I need to create a model Class with three columns: network, graduation, and position(s).我需要创建一个包含三列的model Class :网络、毕业和位置。

class UserProfile(models.Model):
    network = models.ForeignKey(Network)
    graduation = models.IntegerField(blank=True, null=True, choices=choices)
    # position = ?

How would I enter in the position column to allow for multiple positions (for example: artist, architect, graphic artist).我将如何输入 position 列以允许多个职位(例如:艺术家、建筑师、图形艺术家)。 Note that these positions will not have any related information in their own table.请注意,这些职位在他们自己的表格中不会有任何相关信息。

Probably the best way is to go ahead and have a Position model (likely using fixtures to populate the rows):可能最好的方法是 go 前面有一个Position model (可能使用夹具来填充行):

class Position(models.Model):
    label = models.CharField(max_length=100)

    def __unicode__(self):
        return self.label

class UserProfile(models.Model):
    [...]
    positions = models.ManyToManyField(Position)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM