Home > Software engineering >  Withdrawal in Django Rest API
Withdrawal in Django Rest API

Time:08-14

I am getting an error "UserRoom object has no attribute 'images'".

enter image description here

models.py

class UserRoom(models.Model):
    objects = None
    categoty = [
        ('President Lux', 'President Lux'),
        ('Lux', 'Lux'),
        ('Double', 'Double'),
        ('Standard', 'Standard'),
    ]
    name = models.CharField(max_length=150, choices=categoty, verbose_name='Категория')
    room_num = models.CharField(max_length=150,verbose_name='Доступные номера для категории')
    about = models.TextField(verbose_name='Подробности')
    price = models.IntegerField(verbose_name='Цена')
    img360 = models.FileField(verbose_name='Фотография в 360')

    class Meta:
        verbose_name = 'Номер (About)'
        verbose_name_plural = 'Номера (About)'


class UserImg(models.Model):
    objects = None
    name = models.ForeignKey(UserRoom, on_delete=models.CASCADE, verbose_name='img2')
    img = models.FileField(upload_to='User img', verbose_name='Фотография')

    class Meta:
        verbose_name = 'Фотографию'
        verbose_name_plural = 'Фотографии'

views.py

class UserRoomViewSet(ModelViewSet):
    permission_classes = [AllowAny]
    queryset = UserRoom.objects.all()
    serializer_class = UserRoomSer

serializers.py

class UserImgSer(ModelSerializer):
    class Meta:
        model = UserImg
        fields = '__all__'


class UserRoomSer(ModelSerializer):
    images = RelatedField(
        many=True, queryset=UserImg.objects.all())

    class Meta:
        model = UserRoom
        fields = [
            'name', 'room_num', 'about', 'price',
            'img360', 'images',
        ]

urls.py

from rest_framework import routers

router = routers.DefaultRouter()
router.register(r'room', UserRoomViewSet)
urlpatterns = router.urls

Please help me with this problem I can't get json.

{
    "images": [
        {
            "name": "",
            "img": ""
        },
        {
            "name": "",
            "img": ""
        }
    ],
    "name": "",
    "room_num": "",
    "about": "",
    "price": "",
    "img360": ""
}

Where's the problem?

CodePudding user response:

Your problem lies here:

class Meta:
    model = UserRoom
    fields = [
        'name', 'room_num', 'about', 'price',
        'img360', 'images',
    ]

You are trying to refer to 'images' as it was field. But it's not in your model. You can delete it.

Alternatively, you can add them to you model as ManyToManyField:

class UserRoom(models.Model):
    ...
    images = models.ManyToManyField('UserRoomSer', ...)

CodePudding user response:

I think you can just set the related_name field to get the reference from UserRoom model.

class UserImg(models.Model):
    objects = None
    # here I set the related_name attribute in the `name` field
    name = models.ForeignKey(UserRoom, on_delete=models.CASCADE, verbose_name='img2', related_name = 'images') 
    img = models.FileField(upload_to='User img', verbose_name='Фотография')

    class Meta:
        verbose_name = 'Фотографию'
        verbose_name_plural = 'Фотографии'

    class Meta:
        verbose_name = 'Фотографию'
        verbose_name_plural = 'Фотографии'
  • Related