python - django - Can't save model more than once with same user -
i've created model storing track info, mp3, png , slug username associated model.
i can save model fine, when try save 1 (so there 2 models same username), django tells me "track user exists!"
i don't want that, want able save many models want same user.
here models.py (i have registered in admin panel)
from django.db import models django.core.exceptions import validationerror django.core.files.images import get_image_dimensions django.contrib.auth.models import user # create models here. class track(models.model): def generate_user_folder_tracks(instance, filename): return "uploads/users/%s/tracks/%s.mp3" % (instance.user, instance.slug) def is_mp3(self): if not self.name.endswith('.mp3'): raise validationerror(u'you may upload mp3 files tracks!') def generate_user_folder_art(instance, filename): return "uploads/users/%s/art/%s.png" % (instance.user, instance.slug) def is_square_png(self): if not self.name.endswith('.png'): raise validationerror("you may upload png files album art!") else: w, h = get_image_dimensions(self) if not h == w: raise validationerror("this picture not square! picture must equally wide height.") else: if not (h + w) >= 1000: raise validationerror("this picture small! minimum dimensions 500 500 pixels.") return self # variables track_type_choices = [ ('org', 'original'), ('rmx', 'remix'), ('clb', 'collab'), ('liv', 'live'), ] # model fields user = models.foreignkey(user, unique=true) title = models.charfield(max_length=100) desc = models.textfield(max_length=7500) track_type = models.charfield(max_length=3, choices=track_type_choices, default='org') track_type_content = models.charfield(max_length=100,blank=true) created = models.timefield(auto_now=true,auto_now_add=false) upload = models.filefield(upload_to=generate_user_folder_tracks,validators=[is_mp3]) albumart = models.imagefield(upload_to=generate_user_folder_art,validators=[is_square_png]) slug = models.slugfield(max_length=50)
any idea on going on here?
user = models.foreignkey(user, unique=true)
unique = true
means every model needs have different user. similar django's onetoone field.
make sure run
python manage.py makemigrations
and
python manage.py migrate
Comments
Post a Comment