Django manytomany限制 [英] Django manytomany restriction

查看:150
本文介绍了Django manytomany限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库和Django中是新的。我对三位一体的知识很少。



我创建了一个关于学术专家系统的数据库,这里所有论文最多应该有4个主题。如果选择更多的主题,那么应该将警告作为太多话题。一个主题可以被许多论文所拥有。所以这是一种多到四关系。但是我不知道如何将上限限制为4。



我的课程是这样的:

  class Topic(models.Model):
name = models.CharField(max_length = 200)
title = models.CharField(max_length = 200)

class Paper(models.Model):
expert = models.ForeignKey(Expert)
topic = models.ManyToManyField('Topic',related_name ='topic +',blank = True)
coauthors = models.ManyToManyField('Expert',related_name ='coauthors +',blank = True)
title = models.CharField(max_length = 200)
citations = models.ManyToManyField('Paper',related_name ='citations +',blank = True)

def __str __(self):
return self.title


解决方案

我认为您应该在提交表单时检查数据。尝试将这个重载的clean方法放入表单中,而不是格式进入Paper模型的ModelAdmin定义。

 #admin。从django.contrib导入admin 
从django导入$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ (forms.ModelForm):
class Meta:
model = Paper
def clean(self):
topic = self.cleaned_data.get('topic')
如果topic.count()> 4:
raise ValidationError(To many topics!)
return self.cleaned_data

class PaperAdmin(admin.ModelAdmin):
form = PaperForm

admin.register(Paper,PaperAdmin)

此外,这不是最好的方法define related_name。 related_name应该可以帮助你从它的关系中引用模型。这样做:

 #在你的模型
topics = models.ManyToManyField('Topic',related_name =' paper',blank = True)

#shell
paper = Paper.objects.order_by('?')[0]#获取随机对象
paper.topics.all( )#并访问它的主题
topic = Topic.objects.order_by('?')[0]#现在获得随机主题
topic.papers.all()#,并得到它的论文使用related_name!

topic.topic + .all()对吧?我甚至不确定是否可以使用。


I am new in database and Django. I have very little knowledge on trigers.

I created a database about an academic expert system and here all the papers should have up to 4 topics. If more topics are selected than ther should be a warning as "too many topics". A topic could be owned by many papers. So it is kind of many-to-4 relation. But I do not know how to limit the upper bound to 4.

My classes are like this:

class Topic(models.Model):
    name = models.CharField(max_length=200)
    title = models.CharField(max_length=200)

class Paper(models.Model):
    expert = models.ForeignKey(Expert)
    topic = models.ManyToManyField('Topic', related_name='topic+', blank=True)
    coauthors = models.ManyToManyField('Expert', related_name='coauthors+', blank=True)
    title = models.CharField(max_length=200)
    citations = models.ManyToManyField('Paper', related_name='citations+', blank=True)

    def __str__(self):
        return self.title

解决方案

I think you should inspect the data during form submission. Try putting this overloaded clean method into the form and than the form goes into the ModelAdmin definition for your Paper model.

# admin.py
from django.contrib import admin
from django import forms
from django.core.exceptions import ValidationError
from .models import Paper

class PaperForm(forms.ModelForm):
    class Meta:
        model = Paper
    def clean(self):
        topic = self.cleaned_data.get('topic')
        if topic.count() > 4:
            raise ValidationError("To many topics!")
        return self.cleaned_data

class PaperAdmin(admin.ModelAdmin):
    form = PaperForm

admin.register(Paper, PaperAdmin)

Also it is not the best way to define related_name. related_name is supposed to help you refer back to the model from it's relation. Do it like this:

# in your model
topics = models.ManyToManyField('Topic', related_name='papers', blank=True)

# shell
paper = Paper.objects.order_by('?')[0] # get random object
paper.topics.all() # and access it's topics
topic = Topic.objects.order_by('?')[0] # now get random topic
topic.papers.all() # and get it's papers using related_name!

It's better than topic.topic+.all() right? I'm not even sure if it'd work.

这篇关于Django manytomany限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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