These are chat archives for django/django

1st
Nov 2016
Kirill Korovin
@skvoter
Nov 01 2016 10:23
hey
i have some kind of user profile model and i wanna make it more dynamic. i want to have it like /my/media/root/users/(userpic|coverpic)/(user id)/filename
so, what should i do 1) to pass the parameter like if it userpic or cover pic? maybe with string in kwargs, but i don't know how to implement it, i've tried a lot of ways
2) define user id dynamically? the way i did before works fine for a new instance, but if i update picture, it creates a new folder with next id, which is not good
here's a code with some omitted and not needed details
will appreciate any help
def upload_location(instance, filename):
    UserModel = instance.__class__
    id = UserModel.objects.last().id + 1
    return "users/%s/%s" %(id, filename)

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    userpic = models.ImageField(upload_to=upload_location,
        null=True,
        blank=True)
    coverpic = models.ImageField(upload_to=upload_location,
    null=True,
    blank=True)
i have some other ideas with user id, i think it's quite simple, but didn't try it yet. but userpic/coverpic issues really causes a butthurt for me
Jourdan Rodrigues
@jourdanrodrigues
Nov 01 2016 10:30
"instance.pk" is None if the instance is being created.
Or "instance.id", if your PK is the id.
Kirill Korovin
@skvoter
Nov 01 2016 10:33

i think i can use "instance.user.id", because user is always created before the profile, it kinda solves the problem cause profile and user are one-to-one and i have a signal creating profile just after user.

have any ideas with type of picture parameter?

Kirill Korovin
@skvoter
Nov 01 2016 10:46
yeah, issue with user id is solved
but not another
Pami Ketolainen
@keto
Nov 01 2016 10:49
separate upload_location functions for different types... or add pictype parameter and use something like upload_to=functools.partial(upload_location, pictype='coverpic')
Kirill Korovin
@skvoter
Nov 01 2016 10:58
oh, didn't know about functools, should try it!
i think it's what i was looking for, thanks a lot
i think, that separate functions are not really good choice, because i will have pictures for another models, so i'd wanted to have a universal function defining the path
Kirill Korovin
@skvoter
Nov 01 2016 11:08
yeah, it totally works, thanks a lot!
@keto
volksman
@volksman
Nov 01 2016 16:14
I have a formset (NOT modelform), when I POST and try to process it, how do you process only the forms that have changed?
volksman
@volksman
Nov 01 2016 16:22
ahh....form.has_changed() on a per form basis...