Hi!

I'm starting using Django and I have a little problem, may be someone
can help me. Thanks in advance.
My problem:

1. I have 2 models: Places and Photos.
2. There is a ManyToMany relation between Places and Photos through a
custom model 'PlacePhotos'

Class Photo(models.Model):
   ...

Class Places(models.Model):
   photos = models.ManyToManyField(Photo, through='PlacePhotos')
   ...

Class PlacePhotos(models.Model):
   place = models.ForeignKey(Place)
   photo = models.ForeignKey(Photo)
   ...

3. Now, I want to restrict the number of Photos added (for example,
max 25 photos by place)
4. I've created PlacePhotos instances in this way:

   place = get_object_or_404(Place, slug=slug)
   photo = Photo()
   x = PlacePhotos(place=place, photo=photo)
   x.save()

5. I've redefined the PlacePhotos.save() method in this way:


Class PlacePhotos(models.Model):
   place = models.ForeignKey(Place, related_name='place_photos')
   photo = models.ForeignKey(Photo)

   def save(self, request):
      if Photos.objects.filter(place=self.place).count() > 25:
            raise Exception("Too many photos for this place...")
      ##
      ## If everything OK, save the Photo and then save the
relationship.
      ##
      self.photo.save()     ## I save the photo but I don't receive
any id :(
      models.Model.save(self)

5. The problem raises in the last sentence (models.Model.save(self)).
I cannot save the new relationship because self.photo.id = Null so I
cannot save the photo inside this function, I have to save it before
creating the PlacePhotos relationship, but I don't want to save the
new photo if I'm not sure if the quota will be exceeded.

6. Of course I can check the condition count() > 25 before create a
new Photo, but I prefer to do it in the save() method (and avoid to
check the condition in every view).

Any ideas to resolve this please?

Thanks, and sorry for my English mistakes.
x13





-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to