python - Django - Add several Images/Data to one field of a UserProfile model -
i'm creating userprofile model users can add many or few images profile wish. i've considered using image model so:
class image(models.model): related_profile = models.foreignkey(userprofile) # userprofile name of model class user_picture = models.imagefield(upload_to=get_user_img, blank=true, null=true, height_field='height_field', width_field='width_field') when visits userprofile of image objects displayed; when want edit userprofile (i.e. delete image or two) unable this. 'instance' doesn't want return more 1 image object edit error:
get() returned more 1 image -- returned 2!
there's similar question suggested filter() opposed get() here django - get() returned more 1 topic though uses manytomany relationship, , solution didn't work error.
does know ways restructure can edit each model object same page (so not returning aforementioned error)?
like title suggests, i'm wondering if it's possible store set of images list within 1 field of userprofile model because that's potential idea.
you on right track. model.objects.get() method expects query result 1 row (instance), returned. in case userprofile can have number of related image's. need iterate through (possibly) multiple results going query, , each one. more like:
# there 1 userprofile per userid.. say, userid # unique key.. can use get() fetch profile = userprofile.objects.get(userid=the_userid) # need each image image in image.objects.filter(user_profile=profile): # image.... if need image instances , don't need userprofile instance, can shorten join:
for image in image.objects.filter(user_profile__userid=the_userid): # image.... i should add has nothing images, applies time fetch data database using django. query has multiple rows needs done in way.
Comments
Post a Comment