Django Rest Framework: Updating model with ImageField without updating the image - django

Using Django Rest Framework I am trying to update a model with an image field. When I try to update the other fields on the model I get this error:
{"image": ["No file was submitted. Check the encoding type on the form."]}
Here is a simply idea of the serializer. The image field it returns on a GET call just has the file name.
class ModelWithImageSerializer(serializers.ModelSerializer):
image = serializers.ImageField('image', required=False)
class Meta:
model = models.Level
fields = ('id','name', 'image')
How do I update my model without resubmitting the file?

Django REST allows to submit partial PATCH requests (docs). Just make sure to use an UpdateAPIView (or variant of this) which automatically allows this. The idea of partial updates is that they do not require you to submit all model fields which will accomplish the behavior you need.

I was having the same issue with a FileField. I had to delete the FileField property on the PUT request so that it won't override the stored value. Just make sure that blank=True on the Model so that it's an optional field. I'm using DRF 2.4.4.

Make the ImageField as read_only then it will work but when you have post method , then it won't work.

Related

How to sanitize Django Rest Framework inputs

How to sanitize the charfield in Django Rest Framework Using Serializer Or Django models itself. No idea how to secure
This value can be makes my app vulnerable.... Like XSS
<script>alert('Hacked')</script>
Check the serializer code
class Meta:
model = MyDB
fields = ['id','name','price','isAvailable','isVeg','hotelID']
read_only_fields = ['id']
I Have seen Bleach. But it feels like not dynamic, i.e cant be applied to all input field at same time.
Is any option that can be used dynamic. I'm not pro in Django so please suggest me

Django - How to get the max_length of a field

How do I access the max_length of a model field in Django? I am writing a serializer with the REST framework and I want to write a custom validation function to validate the length. Note that i am using SQLite, so the database itself does not enforce the max_length.
You can use the Model _meta API for accessing details of your model's fields.
Here's an example:
class MyModel(models.Model):
title = models.CharField(max_length=200)
# retrieve the max_length
MyModel._meta.get_field('title').max_length
The _meta became a formal API in Django version 1.8. The documentation can be found at https://docs.djangoproject.com/en/stable/ref/models/meta/

Required POST parameter in django or django-rest-framework

How do you enforce a required field when using POST in django view or django-rest-framework view other than this:
required_field = ['id', 'email', 'name']
for f in required_field:
if not f in request.POST:
print "Required field missing"
Is there a better way?
Django REST Framework allows you to specify required=True when initializing the field. If you are using a model, Django REST Framework will automatically detect if the field is required based on if the field has empty=False specified when it was initialized.
When working outside of Django REST Framework, there is no better way of checking for required fields than doing what you are currently doing.

create model formsets from same model but with different model forms

I have an Image model in my Django project. Because of different types of Image I have created three ModelForms according to each type:
class Xray(ModelForm):
#extra_field: Choice Field with specific options for Xray
class Meta:
model = Image
class Internal(ModelForm):
#extra_field: Choice Field with specific options for Internal
class Meta:
model = Image
class External(ModelForm):
#extra_field: Choice Field with specific options for External
class Meta:
model = Image
Each ModelForm has a save logic implemented. I want to create a model formset one for each Image type but want to use the correct ModelForm for each type of Image. I won't use this formset for editing thus I always want it to be empty and have 5 forms(5 items). I can't seem to find in django docs where i can use a specific form for a formset. Only a specific formset (inherit from BaseModelFormSet)
Is it possible to use specific form for each model_formset?
You can do the following:
from django.forms.models import modelformset_factory
from someproject.someapp.models import Image
from someproject.someapp.forms import Internal
ImageFormSet = modelformset_factory(Image, form=Internal)
Here are the docs for modelform_factory, which don't mention the form argument. However, in the "Note" below the examples therein that the function delegates to formset_factory, which is documented to take the form argument. It's just a minor docs issue, and might be a good reason to create a fork of Django, make an update to the docs, and create a pull request.

Admin Form Integration for Custom Model Fields in Django

I need a model field composed of a numeric string for a Django app I'm working on and since one doesn't exist I need to roll my own. Now I understand how "get_db_prep_value" and such work, and how to extend the Model itself (the django documentation on custom model fields is an invaluable resource.), but for the life of me I can't seem to figure out how to make the admin interface error properly based on input constraints.
How do I make the associated form field in the admin error on incorrect input?
Have a look at the Form and field validation section in the Django documentation, maybe that's what you're looking for?
You would have to make a new type of form field for your custom model field.
All you need to do is define a custom modelform which uses your new field, and then tell the admin to use that form to edit your models.
class MyModelForm(forms.ModelForm):
myfield = MyCustomField()
class Meta:
model = MyModel
class MyModelAdmin(admin.ModelAdmin):
form = MyModelForm