I'm developing an API (using DjangoRestFramework) where I want the user to be able to load a list of their items, but I only want to load a few columns from the database (let's say ID, Title, Category, and let's say the URL they are accessing is api/items).
I also want them to be able to send a POST request to this URL containing not only the columns that the page loads, but extra ones as well (example: a form with Title, Category, Date, Rating). If they want to view the item in full, including these extra details, they can view the individual items page (api/items/246).
From my understanding at the moment, the way you select which fields are loaded, is through a serializer class in the model's model.py file. So I have created a listSerializer and a detailSerializer, with the fields I want to load for each listed in the meta class. These work fine for getting the information I want.
My problem is when a user tries to send their POST request adding a new item: the only fields that are saved are the ones listed in the listSerializer's meta class. How can I go about saving the entire object?
Thanks in advance, any help is greatly appreciated.
You can set write_only attribute of fields to True and this will solve exactly your problem. Write only fields will not show on the response but will accept your posted data.
class ListSerializer (serializers.HyperlinkedModelSerializer):
title = serializers.CharField(write_only=True)
Related
i'm trying to make a user registration with multiple fields to save in the same model. The idea is have a form in the home page with username and password and when press button continue the user is redirected to another page with multiple fields to insert personal info for the user profile.
I imagine i must have two different views for that, but i don't understand how put all this data together.
Thanks!
You could use the FormWizard (https://django-formtools.readthedocs.io/en/latest/wizard.html)
Basically you would have one form for user/password and another form for the other fields.
Take a look
In your case i would prefer manipulate HTML content with JavaScript and after everything is ready, just send an ajax call.
I'm creating a UserProfile model where users can add as many or as few images to their profile as they wish. I've considered using an Image model like so:
class Image(models.Model):
related_profile = models.ForeignKey(UserProfile) # UserProfile is the name of the model class
user_picture = models.ImageField(upload_to=get_user_img, blank=True, null=True, height_field='height_field', width_field='width_field')
When somebody visits their UserProfile then all of their Image objects will be displayed; however when I want to edit the UserProfile (i.e. delete an image or two) but am unable to do this.
The 'instance' doesn't want to return more than one Image object to edit as I get error:
get() returned more than one Image -- it returned 2!
There's a similar question like this which suggested filter() as opposed to get() here django - get() returned more than one topic
though this uses a ManyToMany relationship, and the solution didn't work for my error.
Does anybody know any good ways to restructure this so that I can edit each model object from the same page (so not returning the aforementioned error)?
Like the title suggests, I'm wondering if it's possible to store a set of images as a list within one field of the UserProfile model because that's a potential idea.
You are on the right track. The Model.objects.get() method expects the query result to be one row (instance), which is then returned. However in your case the UserProfile can have any number of related Image's. So you need to iterate through the (possibly) multiple results you are going to get back from your query, and do something with each one. More like:
# there is only ONE UserProfile per userid.. that is to say, userid is a
# unique key.. so I can use get() to fetch it
profile = UserProfile.objects.get(userid=the_userid)
# now I need to get each image
for image in Image.objects.filter(user_profile=profile):
# do something with image....
If you only need the Image instances and don't need the UserProfile instance, then you can shorten this with a join:
for image in Image.objects.filter(user_profile__userid=the_userid):
# do something with image....
I should add that this has nothing to do with images, but applies any time you fetch data from the database using Django. Any query that has multiple rows needs to be done in this way.
Assuming I have a model that directly correspond to a ModelFormset.
Assuming three instances of the model are saved in the database.
Assuming I loaded the ModelFormset with initial data = the three instances
Now I render the ModelFormset on a page for users to modify.
After modification, users click on submit. How do I know which one of the ModelFormset correspond to which instance of the Model saved in the database?
Update:
I was reading this example: https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#formsets-initial-data
In this example, the initial data was provide manually. Assuming if the the initial data was passed in like this:
article = Article.objects.get(pk=...)
formset = ArticleFormSet(initial=[
model_to_dict(article)
])
When this formset is sent to the template, is article's id preserved in the rendered HTML? If not, then how does Django know which article it should update if modifications to the article has been made and submitted?
If you have an existing instance in ModelForm (or in a set of them inside ModelFormset), then there's a hidden field with the value of the primary key for the record.
Each ModelForm has also a unique suffix for each the field, which helps distinguish which fields belongs to the same model.
I have two models, Post and Photo. Photo contains information about photos, the date the original photo was uploaded (or alternatively - shot), a description, a title and more. The post contains post text and a ManyToManyField which links back to photos to see which are related to the post.
I however have a problem that, seeing all the photos' titles in list view makes very little sense, and so I must look into a way to show previews.
I have sorl-thumbnails working, but I'm not sure where to start in order to get them into the admin interface.
Help will be very much appreciated!
I have a requirement where one user creates an 'instance' of an object via a ModelForm. Another user of a different group has access to read all of the fields of the form, but has to update only one field. Think of a student who creates an exam object. Then a teach pulls up the exam and just needs to put in a grade, the rest of the exam is read only.
What's the best way to do that? Should I just query for the object, and display each field individually, then create a form (not a ModelForm?) for just the one field?
Should I just query for the object, and display each field individually, then create a form (not a ModelForm?) for just the one field?
This is probably the best way to go about it. Note you can use a ModelForm for the teacher form, see the Django documentation on using a subset of fields on a model form. You will have to display all the other fields manually in your template, but you should probably have a separate template for this view (I would use separate views as well).
You could find some code for a read only field on Django Snippets, but generally it's better to be explicit about what fields you are updating from each view. This is likely to be more trouble than it's worth.