class Hotel (models.Model):
name = models.CharField()
country = models.CharField()
city = models.CharField()
street = models.CharField()
class Residence(models.Model):
hotel_id = models.ForeignKey(Hotel, on_delete=models.DO_NOTHING, related_name='hotel')
house_number = models.CharField()
I created two models Hotel and Residence and I want to join them together
serializers.py
class HotelSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Hotel
fields = ['name', 'country', 'city', 'street']
class ResidenceSerializer(serializers.HyperlinkedModelSerializer):
hotel = HotelSerializer()
class Meta:
model = Residence
fields = ['house_number', 'hotel']
In the serializers.py, I wrote this code
views.py
class ResidenceViewSet(viewsets.ModelViewSet):
serializer_class = ResidenceSerializer
queryset = Residence.objects.all()
In the serializers.py, I wrote this code but when I try to run my code I get this error: Got AttributeError when attempting to get a value for field hotel
on serializer ResidenceSerializer
.
The serializer field might be named incorrectly and not match any attribute or key on the Residence
instance.
Original exception text was: 'Residence' object has no attribute 'hotel'.
Can anyone help me?
CodePudding user response:
can you do the following changes
class RegisterResidenceSerializer(serializers.HyperlinkedModelSerializer):
complex_id = RegisterComplexSerializer()
class Meta:
model = Residence
fields = ['house_number_extension_1', 'complex_id']