1

I have two forms in my views, when I hit on save it's not working properly, when I want to display on my templates what I saved not showing as expected.

Here's what I have:

views.py

def index(request):
    queryset = Personinfo.objects.all()
    queryset2 = Person.objects.all()
    qs = chain(queryset,queryset2)
    form = personform(request.POST or None)
    form2 = personinfoform(request.POST or None)
    context = {
    "queryset": queryset,
    "queryset2": queryset2,
    "qs": qs,
    "form2":form2,
    "form":form,
    }
    form2_valid = form2.is_valid()
    form_valid = form.is_valid()
    if form2_valid and form_valid:
        a = form2.save()
        b= form.save(commit=False)
        b.ForeignkeytoA = a
        b.save()
    return render(request, "index.html", context)

index.html

<form method="POST" action="">{% csrf_token %}
{{form2.as_p}}
{{form.as_p}}
<input type="submit" value="Save!" />
</form>

            <table >
             <tr>
            <th>Name</th>
            <th>Address</th>
            <th>Number</th>
            <th>Hobbies</th>

            </tr>
            {% for item in qs  %}
            <tr> 
                <td>{{ item.name }}</td> #form2
                <td>{{ item.address }}</td> #form1
                <td>{{ item.phone_number }}</td> #form1
                <td>{{ item.address }}</td> #form1
            </tr>
            {% endfor %}
            </table>

models.py

class Personinfo(models.Model):
    name = models.CharField(max_length=128)

    def __str__(self):
        return self.name





class Person(models.Model):
    person = models.ForeignKey(Personinfo)
    address = models.TextField()
    phone_number = models.CharField(max_length=128)
    hobbies =models.CharField(max_length=128)

    def __str__(self):
        return self.person

my output:

Click here

As you can see my table isn't showing my items as expected. Is there a possible way to show every item in the same row?

1 Answer 1

2

Two errors are present. If I understand right, you're expecting the data from a Person instance and the data from its accompanying PersonInfo instance to print on the same line. However, you're trying to achieve this by using chain, which is not joining the querysets based on their relationship, but rather concatenating them blindly.

So if Person.objects.all() returns a queryset which contains the following data

id  person   address   phone_number    hobbies   
1      1         a         a             a
2      2         5         5             5

and PersonInfo.objects.all() returns a queryset which contains

id   Name 
1    aaa      
2    aa       

chain combines them as

id  person  name   address   phone_number    hobbies
1           aaa
2           aa
1      1             a           a             a
2      2             5           5             5

Instead, you should utilize the relationship between the models. If you pass only the Person queryset as context to your template, you could write

{% for p in persons %}
    <tr>
        <td>{{ p.person.name }}</td>
        <td>{{ p.address }}</td>
        <td>{{ p.phone_number }}</td>
        <td>{{ p.hobbies }}</td>
    </tr>
{% endfor %}

--

Additionally you are setting the Personinfo related instance incorrectly when you save your forms. By using b.ForeignkeytoA you are creating a new variable as a member of the object b called ForeignkeytoA, which has nothing to do with the Personinfo relationship. To set the related Personinfo, you should reference the name of the foreign key field, person. To correct this, that segment should be

# ...
b = form.save(commit = False)
b.person = a
b.save()
# ...
Sign up to request clarification or add additional context in comments.

7 Comments

<td>{{p.person.name}} </td> not working for me, I tried passing only Person as you suggested, everything works ok except by name
Try <td>{{ p.person.id }}</td> and see if it prints an id.
Also, is b.ForeignkeytoA = a the way it's written in your code? This will set a new variable called ForeignkeytoA under the python object b, not set the person foreign key to the object a. It should be b.person = a.
What about the ForeignkeytoA?
you meant b.person=a under b.save()?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.