Thursday, October 31, 2013

Re: django - default value name for the ForeignKey

It's more than ok to guive the user reference only to the Album. That is what is called normalization in the relational world.

About your first question, my solution is extract this logic from the model itself. I think it's better to create a service object to do that.

Eg:

class PhotoManager(object):

    def __init__(user):
        self._user = user
    
    def add_photo(photo, album=None):
        if album is None:
            album = self._user.get_default_album()
        album.add_photo(photo)

[]'s
Paulo Poiati

blog.paulopoiati.com


On Thu, Oct 31, 2013 at 1:14 PM, Aamu Padi <aamupadi@gmail.com> wrote:

I have a model for uploading photos in an album by the users. But if the user don't want to create an album, and just upload photos, I want it to be uploaded in a default album, namely 'Default album'. So that, whenever the user uploads photo without creating a new album, the 'Default album should be updated by that photos. For that, I thought about giving a default value in the title of the Album, but I also don't want to create a new album with name 'Default album' whenever the user doesn't create a new album to upload photo. I just want to update the 'Default Album'. Please suggest me how to achieve the above mentioned. And also is it okay to just give the ForeignKey field of the User to the Album class and not the Photo class??? Thank you!

models.py:

class Album(models.Model):      title = models.CharField(max_length=200)      pub_date = models.DateTimeField(default=datetime.now)      user = models.ForeignKey(User)      class Photo(models.Model):      album = models.ForeignKey(Album)      title = models.CharField(max_length=200)      image = models.ImageField(upload_to=get_upload_file_name, blank=True)      pub_date = models.DateTimeField(default=datetime.now)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHSNPWs-6Lx%3Dtwwb0Y%2B7zd4wn-7DEqXUR7%2BE4Ttz4C3wukqFgQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABqSV%3D%2BDDOhW%3DfbWZTdqpONZRdLPY3Db_dBCiqUGQxFne5_NqA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment