Discussion:
Sorry to be "that noob" but help with Create/Post using TemplateHTMLRenderer and a nested serializer?
Toni Marie Swats
2018-11-14 16:42:03 UTC
Permalink
Here's my Stackoverflow question.

https://stackoverflow.com/q/53268218/3507247

bottom line, I have the browsable API working great but I need to render a
frontend form for Create. Frontend form for Update was fairly easy to get
using the DRF official docs. I'm just stumped on the Create form.

I feel stupid, but I'm new and I just want to get this project going.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Carlton Gibson
2018-11-14 16:49:07 UTC
Permalink
Hi Toni

I guess you’re hitting this:

https://github.com/encode/django-rest-framework/issues/5236 <https://github.com/encode/django-rest-framework/issues/5236>

See the PR here:

https://github.com/encode/django-rest-framework/pull/6095 <https://github.com/encode/django-rest-framework/pull/6095>

Basically TemplateRenderer needs a context dict (like a Django template) like this:

```
response.data = {'results': response.data}
```

I remember struggling with this. Nothing noob about it 😀

Kind Regards,

Carlton
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Toni Marie
2018-11-14 17:29:08 UTC
Permalink
Yes I'm sure that's part of it. I tried a totally separate view for create:
class LicensedSoftwareList(APIView):
queryset = LicensedSoftware.objects.all()
serializer_class = LicensedSoftwareSerializer
renderer_classes = [TemplateHTMLRenderer]
template_name = 'licsoftware-detail.html'

def get(self, request, format=None):
licsoftware = LicensedSoftware.objects.all()
serializer = LicensedSoftwareSerializer(licsoftware)
return Response(serializer.data)

def post(self, request, format=None):
serializer = LicensedSoftwareSerializer(data=request.data)
serializer.save()

return redirect('../../licsoftware/')

The error returned is :

Got AttributeError when attempting to get a value for field `software` on serializer `LicensedSoftwareSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `QuerySet` instance.
Original exception text was: 'QuerySet' object has no attribute 'software'.


"software" is the nested model/serializer.

But I also tried it with a non-nested model and I got the same error for a field. Thoughts?
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Carlton Gibson
2018-11-14 18:01:45 UTC
Permalink
Well, you can’t see from what you’ve provided but you LicencedSoftware model doesn’t have a foreign key called software to your Software model.

Sort that out and it will work. (Look at your references and make sure the names match.)

Sent from my iPhone
Post by Toni Marie
queryset = LicensedSoftware.objects.all()
serializer_class = LicensedSoftwareSerializer
renderer_classes = [TemplateHTMLRenderer]
template_name = 'licsoftware-detail.html'
licsoftware = LicensedSoftware.objects.all()
serializer = LicensedSoftwareSerializer(licsoftware)
return Response(serializer.data)
serializer = LicensedSoftwareSerializer(data=request.data)
serializer.save()
return redirect('../../licsoftware/')
Got AttributeError when attempting to get a value for field `software` on serializer `LicensedSoftwareSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `QuerySet` instance.
Original exception text was: 'QuerySet' object has no attribute 'software'.
"software" is the nested model/serializer.
But I also tried it with a non-nested model and I got the same error for a field. Thoughts?
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Toni Marie
2018-11-14 18:10:07 UTC
Permalink
It does, sadly so that isn't it:

class Software (models.Model):
brand = models.CharField(max_length=50, blank=True, null=False)
title = models.CharField(max_length=50, blank=True, null=False,
verbose_name="Software Title")
version = models.CharField(max_length=50, blank=True, null=False)
website = models.CharField(max_length=100, blank=True, null=False)
active = models.BooleanField()
notes = models.CharField(max_length=1000, blank=True, null=True)
assignees = models.ManyToManyField(User, related_name=
'software_assigned', blank=True, verbose_name="Installed Users:")


def __str__(self):
return "{0} {1} Version {2}".format(self.brand, self.title, str(self
.version))




class LicensedSoftware(models.Model):


software = models.OneToOneField(Software, primary_key=True, on_delete=
models.CASCADE)
vehicle = models.CharField(max_length=50, blank=True, null=False,
verbose_name="Contract/Purchase Vehicle")
vendor = models.CharField(max_length=50, blank=True, null=False)
licensekey = models.CharField(max_length=50, blank=True, null=False)
subscription = models.BooleanField()
term = models.CharField(max_length=50, blank=True, null=False)
renewaldate = models.DateField(blank=True, null=True, verbose_name="Renew
By")
supportincluded = models.BooleanField(verbose_name="Support Included")
numpurchased = models.IntegerField(blank=True, null=False, verbose_name="Licenses
Purchased")
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Carlton Gibson
2018-11-14 19:19:25 UTC
Permalink
Looking at your original error, it seems you have a QuerySet where you should have an instance:

Got AttributeError when attempting to get a value for field `software` on serializer `LicensedSoftwareSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `QuerySet` instance.
Original exception text was: 'QuerySet' object has no attribute 'software'.

Most strange. Are you forgetting to call `get()` somewhere?

I can’t tell from the code you’ve posted.
software = models.OneToOneField(Software, primary_key=True,on_delete=models.CASCADE)
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Toni Marie
2018-11-14 19:39:37 UTC
Permalink
Post by Toni Marie
queryset = LicensedSoftware.objects.all()
serializer_class = LicensedSoftwareSerializer
renderer_classes = [TemplateHTMLRenderer]
template_name = 'licsoftware-detail.html'
licsoftware = LicensedSoftware.objects.all()
serializer = LicensedSoftwareSerializer(licsoftware)
return Response(serializer.data)
serializer = LicensedSoftwareSerializer(data=request.data)
serializer.save()
return redirect('../../licsoftware/')
I totally get that I'm NOT doing something right. If someone can figure
it out I even put up a Stackoverflow bounty. I will pay cash money to get
just one working create form that I can replicate for the project (I have
many serializers after this for my app)
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Dipen bhatt
2018-11-15 17:19:44 UTC
Permalink
Hey there Toni, I have posted an answer on stackoverflow. Check it out and
hope it helps. And do mention if i did something you wanted differently.
Peace
Post by Toni Marie
Post by Toni Marie
queryset = LicensedSoftware.objects.all()
serializer_class = LicensedSoftwareSerializer
renderer_classes = [TemplateHTMLRenderer]
template_name = 'licsoftware-detail.html'
licsoftware = LicensedSoftware.objects.all()
serializer = LicensedSoftwareSerializer(licsoftware)
return Response(serializer.data)
serializer = LicensedSoftwareSerializer(data=request.data)
serializer.save()
return redirect('../../licsoftware/')
I totally get that I'm NOT doing something right. If someone can figure
it out I even put up a Stackoverflow bounty. I will pay cash money to get
just one working create form that I can replicate for the project (I have
many serializers after this for my app)
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Toni Marie
2018-11-15 17:44:21 UTC
Permalink
I really appreciate you taking the time to help.

My issue is not with the APIView, it works fine in the API itself.

My issue is a frontend HTML form. The docs on TemplateHTMLRenderer are
very clear on how to create an update form,
https://www.django-rest-framework.org/topics/html-and-forms/#rendering-forms

HOWEVER, creating a blank form to CREATE, I can't seem to pull the correct
"get" method to serialize the form fields.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Toni Marie
2018-11-15 20:12:05 UTC
Permalink
This is finally working. If anyone has a similar issue with
TemplateHTMLRenderer creates, see the above StackOverflow thread. There's
a small glitch that was solved in our discussion, which is to make sure
that the POST response is to redirect to a new url. Which I did in my
application.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...