0

I am trying to create a simple Many to Many relationship. I have two models: IP and IPCollection, an IP can belong to 0 or more collections, and an IPCollection consists of IP addresses.

After following the documentation, I thought I had got it to work. But I could not select existing IP addresses when creating a new collection in the API interface.

Following this post: Lists are not currently supported in HTML input I managed to solve the problem, allowing me to create a new IPCollection while choosing between the existing IP's from a form. Everything seemed to be fine.

However, once I implemented the solution provided in that stackoverflow post, a new problem occured: I can't retrieve my IPs anymore.

I have my two endpoints: /ipcollections and /ips, and whenever I try to do a GET request to /ips I get the following error:

TypeError at /ips/ __init__() takes 1 positional argument but 2 were given

I have tried searching for a solution to this problem, but so far nothing seems to work.

This is wat my serializers look like after implementing the solution from the other stackoverflow post:

class IpSerializer(serializers.PrimaryKeyRelatedField, serializers.ModelSerializer):
    class Meta:
        model = Ip
        fields = ('address',)


class IpCollectionSerializer(serializers.ModelSerializer):

    ipAddresses = IpSerializer(many=True, queryset=Ip.objects.all())

    class Meta:
        model = IpCollection
        fields = ('title', 'ipAddresses')

And my models:

class Ip(models.Model):
    address = models.CharField(max_length=100)

    def __str__(self):
        return self.address

class IpCollection(models.Model):
    title = models.CharField(max_length=100)
    ipAddresses = models.ManyToManyField(Ip, related_name='ipAddresses')

The problem seems to be caused by the 2nd parameter in the IpSerializer, since it works again after removing the PrimaryKeyRelatedField argument. But I can't seem to get the HTML form in the API interface to work in any other way than adding this parameter to the IpSerializer.

Any thoughts/tips on what I might be doing wrong are very welcome!

Max Taylor
  • 343
  • 3
  • 16
  • Since you need to have in API web interface and it is impossible, may be that has not been implemented to have that. But to overcome issue we have option of json / raw input. – Anup Yadav Dec 19 '17 at 11:07
  • Hmm okay, so lets say I remove the PrimaryKeyRelatedField argument, then I get `Lists are not currently supported in HTML input.` in the API web interface. If I want to post data JSON/Raw then it expects me to post with a DICT, and not a primary key. When I try to post as JSON I get the following error: http://prntscr.com/hpilnf – Max Taylor Dec 19 '17 at 11:14
  • You haven't included your VIEW here so confusion got created to me, I thought it is list API but it create API, so now override create method in Serializer in your case IpCollectionSerializer and add your logic (RAW code) to create those models and use Transaction to maintain database. – Anup Yadav Dec 19 '17 at 11:18
  • I can't get the create method to work yet.. I'll try it for a bit and come back on you. But the weird thing to me is: I have IPcollections working, the create API works fine and with HTML form: http://prntscr.com/hpis9x So do you have any idea why this breaks my List API for GET /ips ? Thanks for your help by the way! – Max Taylor Dec 19 '17 at 11:29
  • Sorry for bothering you again, but I can't get it to work at all. I followed the documentation http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers from here, but it doesn't seem to apply for many to many relationships. This is my create method now: http://prntscr.com/hpj2oo but it will give an error: `'ipcollection' is an invalid keyword argument for this function` because I have no property ipcollection on IP model? When I remove the `ipcollection=ipcollection` part, it will make new IP model but 'IpAddresses' property of IPCollection is still empty... – Max Taylor Dec 19 '17 at 11:51
  • there is issue in your question, actually this is not many to many this seems one to many and this is very simple, please confirm, I will put the code in code box. – Anup Yadav Dec 19 '17 at 12:19

0 Answers0