Adhy
Adhy

Reputation: 187

Django get error : id() takes exactly one argument (0 given) ,save with foreign key

So I have a problem about how to save model instance with foreign key relation,

models.py

class Connect(models.Model):
    username = models.CharField(max_length=255)
    password = models.CharField(max_length=255,null=True, blank=True)
    conft = models.TextField(null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return unicode(self.username)


class Ip(models.Model):
    class Meta:
        db_table = 'autonet_ip'

    connect_id = models.ForeignKey(Connect, on_delete=models.CASCADE)
    ipaddr = models.CharField(max_length=255)

    def __str__ (self):
        return self.ipaddr

forms.py

class NacmForm(ModelForm):
    password = forms.CharField(widget=forms.PasswordInput,required = False)
    class Meta:
        model = Connect
        fields = ['username', 'password','conft']

    labels = {'conft':_('Config'),}

class IpForm(ModelForm):

    class Meta:
        model = Ip
        fields = ['ipaddr']
    labels = {'ipaddr':_('IP address'),}


IpFormset = formset_factory(IpForm,  extra=1)

views.py

def konfig(request):
    ip_list = []
    status = ''
    value_bak = 1

    if request.method == 'POST':
        formm = NacmForm(request.POST or None)
        ipform = IpFormset(request.POST)
        upform = UploadForm(request.POST,request.FILES)
        userValue = formm['username'].value()
        passValue = formm['password'].value()
        confValue = formm['conft'].value()
        usernamef = get_object_or_404(Connect, pk=id)
        if ipform.is_valid():
            for form in ipform:
                ipaddr = form.cleaned_data.get('ipaddr')

                //.... some code ...//

                simpanIp = form.save(commit=False)
                simpanIp.connect_id = usernamef
                simpanIp.save()

            simpanForm.save()

        return HttpResponseRedirect('/konfig')
    else:
        formm = NacmForm()
        ipform = IpFormset()
        return render(request, 'konfig.html', {'form': formm, 'logins': Connect.objects.all(), 'ipform': ipform, 'status': status })

Then, when I input all data and click submit to collect form data and on simpanIp.save(), I've got an error: id() takes exactly one argument (0 given).

I just want to know how to save the instance of Connect model to database with foreign key, thanks in advance


so i edit my models.py like this

class Connect(models.Model):
......
        def get_usernameinf(self):
            return ', '.join(self.usernameinf.all().values_list('username', flat=True))

and views.py like this

if request.method == 'POST':
.....some code.....

if ipform.is_valid() and formm.is_valid():
    simpanForm = formm.save()

    for form in ipform:
    simpanIp = form.save(commit=False)
    ...... some code ..
        simpanIp.connect_id = simpanForm
        simpanIp.save()

and its work, now the result is my "connect_id" got value from "Connect id"

Upvotes: 0

Views: 467

Answers (1)

Dunes
Dunes

Reputation: 40683

id is a Python builtin that gives a unique ID for an object. I would guess that you did not intend to pass it get_object_or_404 on this line:

get_object_or_404(Connect, pk=id)

The calling convention for this functions seems to be that it is meant to be an integer for the primary key in a database table. Figure out where you should be getting your primary key from and set it correctly.

Pro-tip: avoid using names that are predefined by Python (see here for a full list). It can lead to headaches like the one you just had.

Upvotes: 1

Related Questions