关注照片应用教程时出现Django models.py错误

时间:2012-09-14 19:26:47

标签: python django django-models

我正在浏览以下tutorial,并且models.py代码似乎已被破坏(我在Mac和Django 1.4.1上)。

这是models.py代码:

from django.db import models

from django.contrib.auth.models import User
from django.contrib import admin

class Album(models.Model):
    title = models.CharField(max_length=60)
    public = models.BooleanField(default=False)
    def __unicode__(self):
        return self.title

class Tag(models.Model):
    tag = models.CharField(max_length=50)
    def __unicode__(self):
        return self.tag

class Image(models.Model):
    title = models.CharField(max_length=60, blank=True, null=True)
    image = models.FileField(upload_to='images/')
    tags = models.ManyToManyField(Tag, blank=True)
    albums = models.ManyToManyField(Tag, blank = True)
    created = models.DateTimeField(auto_now_add=True)
    rating = models.IntegerField(default=50)
    width = models.IntegerField(blank=True, null=True)
    height = models.IntegerField(blank=True, null=True)
    user = models.ForeignKey(User, null=True, blank=True)
    def __unicode__(self):
        return self.image.name

    def save(self, *args, **kwargs):
        """Save image dimensions."""
        super(Image, self).save(*args, **kwargs)
        im = PImage.open(os.path.join(MEDIA_ROOT, self.image.name))
        self.width, self.height = im.size
        super(Image, self).save(*args, ** kwargs)

    def size(self):
        """Image size."""
        return "%s x %s" % (self.width, self.height)

    def __unicode__(self):
        return self.image.name

    def tags_(self):
        lst = [x[1] for x in self.tags.values_list()]
        return str(join(lst, ', '))

    def albums_(self):
        lst = [x[1] for x in self.albums.values_list()]
        return str(join(lst, ', '))

    def thumbnail(self):
        return """<a href="/media/%s"><img border="0" alt="" src="/media/%s" height="40" /></a>""" % (
                                                                    (self.image.name, self.image.name))
    thumbnail.allow_tags = True

class AlbumAdmin(admin.ModelAdmin):
    search_fields = ['title']
    list_display = ['title']

class TagAdmin(admin.ModelAdmin):
    list_display = ['tag']

class ImageAdmin(admin.ModelAdmin):
    search_fields = ['title']
    list_display = ["__unicode__", "title", "user", "rating", "size", "tags_", "albums_",
        "thumbnail", "created"]
    list_filter = ["tags", "albums", "user"]

    def save_model(self, request, obj, form, change):
        obj.user = request.user
        obj.save()

admin.site.register(Album, AlbumAdmin)
admin.site.register(Tag, TagAdmin)
admin.site.register(Image, ImageAdmin)

这是我尝试同步数据库时遇到的错误:

Error: One or more models did not validate:
photoapp.image: Accessor for m2m field 'tags' clashes with related m2m field 'Tag.image_set'. Add a related_name argument to the definition for 'tags'.
photoapp.image: Accessor for m2m field 'albums' clashes with related m2m field 'Tag.image_set'. Add a related_name argument to the definition for 'albums'.

任何帮助都非常感激。

2 个答案:

答案 0 :(得分:2)

您的M2MTag重复albums,我想要为Album更改tags = models.ManyToManyField(Tag, blank=True) albums = models.ManyToManyField(Tag, blank = True,) 。也有好主意也有相关名称

tags = models.ManyToManyField(Tag, blank=True, related_name="img_tags")
albums = models.ManyToManyField(Album, blank = True, related_name="img_albums")

更改为

{{1}}

答案 1 :(得分:0)

如果有两个字段是同一模型的外键,则需要提供related_name,以便django知道要将该关系称为什么。这就是错误所说的。

因此,通过为您的关系提供一个独特的名称来更新您的模型:

tags = models.ManyToManyField(Tag, blank=True, related_name='image_tags')
albums = models.ManyToManyField(Tag, blank = True, related_name='album_tags')

但是,您可能的含义是:

tags = models.ManyToManyField(Tag, blank=True)
albums = models.ManyToManyField(Album, blank=True)