django - default value name for the ForeignKey

260 views
Skip to first unread message

Aamu Padi

unread,
Oct 31, 2013, 11:14:57 AM10/31/13
to django...@googlegroups.com

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)

Paulo Gabriel Poiati

unread,
Oct 31, 2013, 11:39:41 AM10/31/13
to django...@googlegroups.com
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


--
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...@googlegroups.com.
To post to this group, send email to django...@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.

Aamu Padi

unread,
Oct 31, 2013, 11:49:16 AM10/31/13
to django...@googlegroups.com
Thank you so much for your reply. But I am just a beginer, didn't quite understood the 'Manger' thing. Will you please be kind enough to elaborate a little more. Thank you.
Reply all
Reply to author
Forward
0 new messages