-
Notifications
You must be signed in to change notification settings - Fork 301
UnboundLocalError in renderers.py #270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I believe the problem has to do with including reverse relationships. My resource at class AbonneFichierSerializer(serializers.ModelSerializer):
frequences_recu_fiscal = FrequenceRecuFiscalAbonneFichierSerializer(many=True)
included_serializers = {
'fichier': FichierSerializer,
'abonne_details': AbonneDetailsSerializer,
'abonne': AbonneSerializer,
'frequences_recu_fiscal': FrequenceRecuFiscalAbonneFichierSerializer
}
class Meta:
model = AbonneFichier If I comment out the two lines that mention class FrequenceRecuFiscalAbonneFichier(Base):
id = models.AutoField(primary_key=True)
abonne_fichier = models.ForeignKey(AbonneFichier,
on_delete=models.CASCADE,
related_name='frequences_recu_fiscal')
type_recu_fiscal = models.ForeignKey(TypeRecuFiscal, on_delete=models.PROTECT)
frequence_recu_fiscal = models.ForeignKey(FrequenceRecuFiscal, on_delete=models.PROTECT) I also noticed that in the code of the function if isinstance(field, relations.HyperlinkedIdentityField):
#...
if isinstance(field, ResourceRelatedField):
#...
if isinstance(field, (relations.PrimaryKeyRelatedField, relations.HyperlinkedRelatedField)):
#...
if isinstance(field, relations.ManyRelatedField):
#... Then on the following condition: if isinstance(field, ListSerializer) and relation_instance is not None:
|
@daveslab thanks for the detailed report. I believe that |
@daveslab I'm surprised that a test didn't catch this regression. If you want to add a test to verify the fix this would be a good place for it. |
@jerel Thanks for responding so quickly. As you asked, if I change line 202 to be if isinstance(field, ListSerializer): Then it works just fine. How do I add a test? (To be honest, like I said, I'm a serious n00b and don't even know how to use git). |
@jerel Ahh, however, I just noticed another problem (not sure if I should open another issue), but now that the call works when I change line 202, it doesn't return the right //...
"type": "AbonneFichier",
"id": "20007",
"attributes": {
//...
},
"relationships": {
"frequences_recu_fiscal": {
"data": [
{
"type": "FrequenceRecuFiscalAbonneFichier",
"id": "18470"
},
//...
]
},
//... But what it returns is: //...
"type": "AbonneFichier",
"id": "20007",
"attributes": {
//...
},
"relationships": {
"frequences_recu_fiscal": {
"data": [
{
"type": "AbonneFichier",
"id": "18470"
},
//...
]
},
//... |
Hi, I'm sorry to bother, but I could have someone's thoughts on this? I'm happy to make the slight change necessary to fix the original bug I reported in this issue, but this second problem seems more difficult to debug. |
@daveslab Ill see if I can track it down. I think I managed to get the tests to break from this issue. EDIT: NM it was unrelated test breakage. |
@daveslab Can you try changing frequences_recu_fiscal = FrequenceRecuFiscalAbonneFichierSerializer(many=True) to from rest_framework_json_api import relations
...
frequences_recu_fiscal = relations.ResourceRelatedField(source='frequences_recu_fiscal', many=True) Im not sure embedding a serializer like you are doing is officially supported without a corresponding EDIT: See #256 for how to default include vs parameterly include |
@scottfisk Thanks so much for replying Scott. I made the changes you suggested and now it works if I delete the second part of the line 202 as was suggested to me above. Now my serializer looks like this (I've since had to add another field, class AbonneFichierSerializer(serializers.ModelSerializer):
frequences_recu_fiscal = ResourceRelatedField(many=True, read_only=True)
statuts = ResourceRelatedField(many=True, read_only=True)
included_serializers = {
'fichier': FichierSerializer,
'abonne_details': AbonneDetailsSerializer,
'abonne': AbonneSerializer,
'frequences_recu_fiscal': FrequenceRecuFiscalAbonneFichierSerializer,
'statuts': StatutSerializer,
}
class Meta:
model = AbonneFichier Originally I was following this post on SO by the creator of DRF on how to include related models. At the end of the day, it looks like deleting that stuff on line 202 in |
+1 to this. For reference, here is a simplified version of my use case:
And the endpoint for
|
I couldn't get a failing test going for this, maybe this simple test case will help me. |
Hopefully! |
@jarekwg Well I found out that the tests use I might have to defer to @jerel on what the difference between these 2 imports are and if things are expected to work importing and using drf |
|
@scottfisk @jerel I'd still label this as a bug currently. If drf serializers are not to be used with jsonapi viewsets, then an exception should be raised much earlier on, rather than this subtle mistake in the output. However, I really hope there's a better solution than just not supporting drf serializers. Part of the beauty of this package is that it can be used as a partial drop-in replacement to drf and everything just works. Here's my company's use case: I'm not sure exactly how jsonapi's From doing some of my own digging I see that Anyway, I see two courses of action here:
If I submit a PR for the second option will you accept it? :) |
I would be grateful for a PR for option two. Using JSON-API serializers and default serializers in the same app should On Sep 7, 2016 6:14 PM, "Jarek Glowacki" [email protected] wrote:
|
what jerel said, option 2 works for me. It seems like the current situation is more of they aren't really tested and therefore not officially supported. As you noticed most use cases using the drf serializers directly worked prior to 2.1.0 so its certainly possible to support them, not sure what edge cases would fail yet. Maybe we should open another issue for the |
Done, sorry for derailing. :) |
Closed by #278 |
I recently upgraded my project to Django 1.10 and to DRF JSON API 2.1.0, and I am now seeing an error every time I try to make a simple GET call on one of my resources. The error remains if I use Django 1.10 or Django 1.9. Here is a piece of the error page:
The error comes from the function
extract_relationships
in the classJSONRenderer
. I'm still a newbie to Django and DRF so I'm having a hard time debugging the function. I think the problematic commit might be b96d79d.The text was updated successfully, but these errors were encountered: