最初从这里开始:Django IN query as a string result - invalid literal for int() with base 10
我的网站中有许多应用,目前正在使用简单的“博客”应用。我开发了一个“收藏”应用程序,很容易,利用Django中的ContentType框架让我拥有任何类型的“最爱”...试图走另一条路,然而,我不知道我是什么我在做什么,找不到任何例子。
我将从最喜欢的模特开始:
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.contrib.auth.models import User
class Favorite(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
user = models.ForeignKey(User)
content_object = generic.GenericForeignKey()
class Admin:
list_display = ('key', 'id', 'user')
class Meta:
unique_together = ("content_type", "object_id", "user")
现在,这允许我循环浏览收藏夹(例如,在用户的“收藏夹”页面上),并通过{{favorite.content_object.title}}获取相关的博客对象。
我现在想要的,也无法弄清楚,我需要对博客模型做些什么才能让我对收藏夹有一些限制(因此当它显示在列表中时,它可以突出显示,因为例子)。
以下是博客模型:
from django.db import models
from django.db.models import permalink
from django.template.defaultfilters import slugify
from category.models import Category
from section.models import Section
from favorite.models import Favorite
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
class Blog(models.Model):
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=140, editable=False)
author = models.ForeignKey(User)
homepage = models.URLField()
feed = models.URLField()
description = models.TextField()
page_views = models.IntegerField(null=True, blank=True, default=0 )
created_on = models.DateTimeField(auto_now_add = True)
updated_on = models.DateTimeField(auto_now = True)
def __unicode__(self):
return self.title
@models.permalink
def get_absolute_url(self):
return ('blog.views.show', [str(self.slug)])
def save(self, *args, **kwargs):
if not self.slug:
slug = slugify(self.title)
duplicate_count = Blog.objects.filter(slug__startswith = slug).count()
if duplicate_count:
slug = slug + str(duplicate_count)
self.slug = slug
super(Blog, self).save(*args, **kwargs)
class Entry(models.Model):
blog = models.ForeignKey('Blog')
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=140, editable=False)
description = models.TextField()
url = models.URLField(unique=True)
image = models.URLField(blank=True, null=True)
created_on = models.DateTimeField(auto_now_add = True)
def __unicode__(self):
return self.title
def save(self, *args, **kwargs):
if not self.slug:
slug = slugify(self.title)
duplicate_count = Entry.objects.filter(slug__startswith = slug).count()
if duplicate_count:
slug = slug + str(duplicate_count)
self.slug = slug
super(Entry, self).save(*args, **kwargs)
class Meta:
verbose_name = "Entry"
verbose_name_plural = "Entries"
任何指导?
答案 0 :(得分:2)
django doc就在这里:Reverse generic relations。基本上在Blog模型本身上,您可以添加GenericRelation
...
class Blog(models.Model):
favorites = generic.GenericRelation(Favorite)
对于给定的博客,您可以找到与其关联的所有Favorite
模型......
b = Blog.objects.get(slug='hello-world-blog-slug')
all_blog_favorites = b.favorites.objects.all()
或者查看当前用户是否有博客收藏...
user_has_blog_favorited = b.favorites.objects.filter(user=request.user).exists()