如何在ForeignKey字段中使用Django中自动创建的隐式模型类? [英] How can I use the automatically created implicit through model class in Django in a ForeignKey field?

查看:164
本文介绍了如何在ForeignKey字段中使用Django中自动创建的隐式模型类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django中,我有以下模型。



在Supervisor模型中,我有一个多对多的字段,没有通过表明确定义。在主题模型的ForeignKey字段中,我想参考自动创建的中间模型(由Supervisor模型中的多对多字段创建),但我不知道中间模型的名称(所以我写了'???',而不是这个名字)。



Django文档告诉如果你没有指定一个显式的通过模型,还有一个隐含的模型类可以直接访问创建的表以保存关联。



如何在ForeignKey字段中使用Django中自动创建的隐式模型类? / p>

  import d 
from django.db import models

class TopicGroup(models.Model) :
title = models.CharField(max_length = 500,unique ='True')

def __unicode __(self):
re转动re.sub(r'^(。{75})。* $','\g< 1> ...',self.title)

class Meta:
order = ['title']

class Supervisor(models.Model):
name = models.CharField(max_length = 100)
neptun_code = models.CharField(max_length = 6 )
max_student = models.IntegerField()
topicgroups = models.ManyToManyField(TopicGroup,blank = True,null = True)

def __unicode __(self):
返回u'%s(%s)'%(self.name,self.neptun_code)

class Meta:
ordering = ['name']
unique_together =(' name','neptun_code')

class主题(models.Model):
title = models.CharField(max_length = 500,unique ='True')
foreign_lang_requirements = .CharField(max_length = 500,blank = True)
note = models.CharField(max_length = 500,blank = True)
supervisor_topicgroup = models.ForeignKey(???,blank = True,null = True )

def __unicode __( self):
return u'%s ---%s'%(self.supervisor_topicgroup,re.sub(r'^(。{75})。* $','\g&1。 ..',self.title))

class Meta:
ordering = ['supervisor_topicgroup','title']


解决方案

只是通过调用 - 所以在你的情况下,$ $ c> Supervisor.topicgroups.through



虽然我认为如果你要在你的主题模型中明确指出它您可以直接声明为模型。


In Django I have the following models.

In the Supervisor model I have a many-to-many field without an explicitly defined through table. In the ForeignKey field of the Topic model I would like to refer to the automatically created intermediate model (created by the many-to-many field in the Supervisor model), but I don't know what is the name of the intermediate model (therefore I wrote '???' there, instead of the name).

Django documentation tells that "If you don’t specify an explicit through model, there is still an implicit through model class you can use to directly access the table created to hold the association."

How can I use the automatically created implicit through model class in Django in a ForeignKey field?

import re
from django.db import models

class TopicGroup(models.Model):
    title = models.CharField(max_length=500, unique='True')

    def __unicode__(self):
        return re.sub(r'^(.{75}).*$', '\g<1>...', self.title)

    class Meta:
        ordering = ['title']

class Supervisor(models.Model):
    name = models.CharField(max_length=100)
    neptun_code = models.CharField(max_length=6)
    max_student = models.IntegerField()
    topicgroups = models.ManyToManyField(TopicGroup, blank=True, null=True)

    def __unicode__(self):
        return u'%s (%s)' % (self.name, self.neptun_code)

    class Meta:
        ordering = ['name']
        unique_together = ('name', 'neptun_code')

class Topic(models.Model):
    title = models.CharField(max_length=500, unique='True')
    foreign_lang_requirements = models.CharField(max_length=500, blank=True)
    note = models.CharField(max_length=500, blank=True)
    supervisor_topicgroup = models.ForeignKey(???, blank=True, null=True)

    def __unicode__(self):
        return u'%s --- %s' % (self.supervisor_topicgroup, re.sub(r'^(.{75}).*$', '\g<1>...', self.title))

    class Meta:
        ordering = ['supervisor_topicgroup', 'title']

解决方案

It's just called through - so in your case, Supervisor.topicgroups.through.

Although I think that if you're going to be referring to it explicitly in your Topic model, you might as well declare it directly as a model.

这篇关于如何在ForeignKey字段中使用Django中自动创建的隐式模型类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆