OPTIONS and ForeignKey model field

143 views
Skip to first unread message

Vermus Rus

unread,
Jun 14, 2015, 6:06:16 AM6/14/15
to django-res...@googlegroups.com
Hello!
How to disable list of ForeignKey model field objects in OPTION method?

f.e.
Model:

class Model(models.Manager):
       address
= models.ForeignKey(Address)


class ModelSerializer(serializers.ModelSerializer):
   
class Meta:
        model
= Model
        fields
= 'address'


Then we use 

class ModelList(generics.GenericAPIView,
                      mixins
.CreateModelMixin,
                      mixins
.ListModelMixin)      


in a view. 

When we have Meta, than we have access to OPTIONS HTTP method.

When we request OPTIONS we got like a

"address": {
....
"choices": [{
 
"id": "1",
}, {
 
"id": "2",
}, {
 
"id": "3",
} etc ]...
}

So, it Address.objects.all() in choices, but i don't want show address list at all. And i want to set any address (we know id, for example), when i save (create, update) Model instance.

If we use "address = PrimaryKeyRelatedField(queryset=Address.objects.none())"  then OPTIONS is ok, but POST\PUT is none: "The queryset used for model instance lookups when validating the field input". ( http://www.django-rest-framework.org/api-guide/relations/ )

Should i use:
class ModelSerializer(serializers.ModelSerializer):

     address_pk = serializers.IntegerField(source='address')

   
class Meta:
        model
= Model
        fields
= 'address_pk'

instead? But what about ManyToManyField in this case?




Vermus Rus

unread,
Nov 18, 2015, 5:45:41 AM11/18/15
to Django REST framework
Ok, we just can use two separate Serializers for Options and other methods

воскресенье, 14 июня 2015 г., 13:06:16 UTC+3 пользователь Vermus Rus написал:
Reply all
Reply to author
Forward
0 new messages