ForeignKey ManyToManyField 中rela
# -*- coding:utf-8 -*-
from django.db import models
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
# Create your models here.
class UserProfile(models.Model):
user = models.OneToOneField(User,unique=True)
name = models.CharField(max_length=32,verbose_name=u'昵称')
age = models.SmallIntegerField(verbose_name=u'年龄',default=0)
sex_type = {(0,u'男'),
(1,u'女')
}
sex =models.IntegerField(choices=sex_type,default=0,)
signature = models.CharField(max_length=128,verbose_name=u'个性签名',blank=True,null=True)
def __str__(self):
return self.name
class Article(models.Model):
title = models.CharField(max_length=255,verbose_name=u'标题')
content = models.TextField(verbose_name=u'内容')
author = models.ForeignKey("UserProfile",verbose_name=u'作者')
published_data = models.DateTimeField(auto_now_add= True,verbose_name=u'发布时间')
modify_data = models.DateTimeField(auto_now = True,verbose_name=u'更新时间')
art_img = models.ImageField(width_field=150,height_field=150,upload_to="uploads",blank=True,null=True)
category = models.ForeignKey("Category",verbose_name=u'所属栏目',related_name='myarticle')
def __str__(self):
return self.title
class Category(models.Model):
name = models.CharField(max_length=32,verbose_name=u'栏目名称')
brief = models.CharField(null=True, blank=True, max_length=255,verbose_name=u'栏目简介')
set_as_top_menu = models.BooleanField(default=False)
position_index = models.SmallIntegerField()
url_name = models.CharField(max_length=32,verbose_name=u'栏目网址')
def __str__(self):
return self.name
class Meta:
verbose_name = u'栏目'
verbose_name_plural = u'栏目'
class Comment(models.Model):
content = models.CharField(max_length=255,verbose_name=u'评论内容',blank=True,null=True)
author = models.ForeignKey('UserProfile',related_name='mycomment'verbose_name=u'评论人')
data = models.DateTimeField(auto_now_add=True,verbose_name=u'评论时间')
article = models.ForeignKey("Article",verbose_name=u'所属文章')
parent_comment = models.ForeignKey('self',related_name='son_comment',verbose_name=u'上级评论',blank=True,null=True)
comment_type_choice = {
(0,u'评论'),
(1,u'点赞')
}
comment_type = models.IntegerField(choices=comment_type_choice,default=0,verbose_name=u'评论类型')
def __str__(self):
return self.content
def clean(self):
if self.comment_type == 0 and len(self.content)== 0 :
raise ValidationError(u'评论不能为空')
ForeignKey ManyToManyField 中 related_name 可加可不加,但是一个字段里有多个ForeignKey 指向同一个字段,则必须添加related_name来区分。以上面代码为例:
在字段里Article里可通过category属性来查询所属栏目:
article_obj = models.Article.objects.get(id=1) #取id为1的文章
article_obj.category 查出所属栏目
but,如何通过栏目来查出这个栏目下所有的文章呢?
category_obj = models.Category.objects.get(id=1) #取ido为1的栏目
category_obj.myarticle.all()或者category_obj.myarticle.select_related()来查出栏目1下的所有文章。
,如果Article字段属性category没有添加related_name,可通过model_name+'_set'这种访问方式,使用方法如下:
category_obj.article_set.all() 或者 category_obj.article_set.select_related()
另外 ForeignKey ManyToManyField 对象可以是字段本身,使用方法为:
parent_comment = models.ForeignKey('self',related_name='son_comment',verbose_name=u'上级评论',blank=True,null=True)
models中clean方法的使用:
from django.core.exceptionsimportValidationError
defclean(self):if self.comment_type == 0and len(self.content)==0 :raise ValidationError(u'评论不能为空')
这样就规定了如果回复类型是评论的话 必须要有内容 否则后台就会报错。