python - Limit ForeignKey models by count -


this model , want limit number of photos user can upload 10. want 1 place works in admin , user facing forms. can me out here?

class starphotos(models.model):      user = models.foreignkey(user, on_delete=models.cascade)     photo_category = (     ('hs', "head shot"),     ('wp', "western party wear"),     ('ip', "indian party wear"),     ('sw', "swim wear"),     ('cw', "casual wear"),     )     category = models.charfield(max_length=2, choices=photo_category, default='cw') # filefield should preferaby changed imagefield pillow installed.     photos = models.filefield(max_length=200, upload_to='images/',)      def __str__(self):         return "images {0}".format(self.user) 

you can use checker function in model check whether user has uploaded 10 photos or not

def check_photo_count(self, user):     photo_count = self.object.filter(user=user).count()     return photo_count 

this not seem best solution available should work. remember put check in views or admin. can return boolean value function saying user allowed upload more photos or not.

this same logic can applied manager if don't want put checks everywhere.

so have put check in create method , if check fails raise error or return false value saying object not created.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -