I am going through the following tutorial, and the models.py code seems to be broken (I’m on a Mac and Django 1.4.1).
Here’s the models.py code:
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)
Here’s the error I’m getting when I try to sync the database:
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'.
Any help much appreciated.
You have duplicate
M2MwithTagasalbumsyou my want to change that forAlbum. Also good idea to have related name as wellchange to