Sending a PUT request to update data for one of my models but I get an error that a two fields are required even though they are set to blank=True and null=True in my model. When I create it works fine, it is just update. Maybe it has to do with using generic views? Using the same form in React for creating a pet and updating a pet information:
export const UpdatePetInfo = async (petId, petInfo) => {
try {
const res = await Client.put(`pets/${petId}`, petInfo)
console.log(res)
return res.data
} catch (error) {
throw error
}
}
const handleSubmit = async (e) => {
e.preventDefault()
if (editPetForm) {
console.log(selectedPet)
console.log(editPetForm)
await UpdatePetInfo(selectedPet.id, {
name: formValues.name,
animal_group: formValues.animal_group,
animal_kind: formValues.animal_kind,
dob: formValues.dob,
gotcha_date: formValues.gotcha_date,
age: formValues.age,
user_id: user.id
})
navigate(`/dash/${selectedPet.id}`)
} else {
await CreatePet({
name: formValues.name,
animal_group: formValues.animal_group,
animal_kind: formValues.animal_kind,
dob: formValues.dob,
gotcha_date: formValues.gotcha_date,
age: formValues.age,
user_id: user.id
})
// Redirects user to login
navigate('/pets')
}
// Resets the form to blank once API req completes successfully
setFormValues({
name: '',
animal_group: '',
animal_kind: '',
dob: '',
gotcha_date: '',
age: ''
})
setEditPetForm(false)
}`
models.py
class Pet(models.Model):
GROUP_CHOICES = (
('Mammal', 'Mammal'),
('Bird', 'Bird'),
('Fish', 'Fish'),
('Reptile', 'Reptile'),
('Amphibian', 'Amphibian'),
('Other', 'Other')
)
user = models.ForeignKey(
User, on_delete=models.CASCADE, related_name='pets')
name = models.CharField(max_length=100)
animal_group = models.CharField(max_length=100, choices=GROUP_CHOICES)
animal_kind = models.CharField(max_length=100)
dob = models.DateField(help_text='<em>YYYY-MM-DD</em>',
blank=True, verbose_name='DOB', null=True)
gotcha_date = models.DateField(help_text='<em>YYYY-MM-DD</em>')
age = models.PositiveIntegerField(blank=True, null=True)
def __str__(self):
return self.name
views.py
class PetDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Pet.objects.all()
serializer_class = PetSerializer
Using react for front end and django rest framework for backend. Please let me know if there is anything I may have missed help solve this issue. I am pretty beginner at coding, I appreciate any help!
Thank you!