I am having a strange error. I have a view called WrongMatch, which people using my app can suggest a change if a link is wrong. This is done over a POST request.
When I make a WrongMatch inside the browsable API, I can enter its detail view and the PUT html form is filled correctly, but for the ones coming from the POST request, the first field in the PUT html form defaults to the first entry in the database. However, both entries looks identical to me.
This picture shows two entries, the first one is POSTed from the app, and the second is made in the browsable API.
This shows the detail view of the first entry, but as seen in the PUT html form, the beer defaults to the first in the database and not the one in the entry.
This shows the detail view of the second entry, added from the browsable API. Here the field in the PUT html form shows correctly.
The viewset used is quite simple.
class WrongMatchViewSet(ModelViewSet):
queryset = WrongMatch.objects.all()
serializer_class = WrongMatchSerializer
pagination_class = Pagination
permission_classes = [permissions.AllowAny]
And the serializer:
class WrongMatchSerializer(serializers.ModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name="wrongmatch-detail")
beer_name = serializers.CharField(read_only=True, source="beer.vmp_name")
current_untpd_url = serializers.CharField(read_only=True, source="beer.untpd_url")
current_untpd_id = serializers.CharField(read_only=True, source="beer.untpd_id")
class Meta:
model = WrongMatch
fields = [
"url",
"beer",
"beer_name",
"current_untpd_url",
"current_untpd_id",
"suggested_url",
"accept_change",
]
def update(self, instance, validated_data):
instance.beer = self.validated_data["beer"]
instance.suggested_url = self.validated_data["suggested_url"]
instance.accept_change = self.validated_data["accept_change"]
if instance.accept_change == True:
b = instance.beer
b.untpd_url = instance.suggested_url
b.untpd_id = instance.suggested_url.split("/")[-1]
b.prioritize_recheck = True
b.verified_match = True
b.match_manually = False
b.save()
instance.delete()
return instance
Anyone have any clue as to why this is happening? The beer id is correct from the post entry, and the beer_name and other information also matches the correct beer. The idea is for me to just go through these entries, set Accept Change to true and the change happens automatically, but when the beer entry is wrong in the PUT form it makes a mess and put the information somewhere it does not belong.