django模型:如何将一个类的id和一个类的非空对象字段传递给另一个类?

abithluo  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(224)

这是一个概念性的问题,不一定是一个错误。如何区分绘制类的自动生成id和绘制类中非空对象字段的名称?例如,在我的类源中,我有:

class Source(models.Model):
    profile = AutoOneToOneField(User, on_delete=models.CASCADE, null=True)
    project= AutotOneToOneField(Project, on_delete=models.CASCADE, null=True)
    team =AutotOneToOneField(Team, on_delete=models.CASCADE, null=True)
    department = AutoOneToOneField(Department, on_delete=models.CASCADE, null=True)

  def __str__(self):
        return self.profile + self.project + self.team + self.department

我们的目标是让源文件来自一个概要文件、项目、团队或部门,我想将该源文件与一个sourceid相关联,但我也想知道源文件的类型。在我的课堂上,videopost:

class VideoPost(models.Model):
    sourceID = models.ForeignKey(Source, on_delete=models.CASCADE)
    postID = models.ForeignKey(Post, on_delete=CASCADE)

我想绘制sourceid,但在我的类中:

class Following(models.Model): 
    profile = AutoOneToOneField(User, on_delete=models.CASCADE)  
    account_types = models.ForeignKey(Source, on_delete=models.CASCADE)  #need to create an account type
    account_ids = models.PositiveIntegerField()
    following_objects = GenericForeignKey('account_types', 'account_ids')

    def __str__(self):
        return self.profile + 'is following'

我要绘制源的类型(在帐户类型中)。如何区分和返回所需的值?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题