Django - How to interact 2

Asked

Viewed 21 times

0

In Form1 I have this entry "name_car". This form saves normally in the database. In form2 I have the same field "name_car" to put the "pieces". Question: How to bring the Form1 car name to the form2 field without having to select in the field.

no models.py are related.

py.models

class car(models.Model):
    name_car= models.CharField(max_length=100) 
    nome_model= models.CharField(max_length=100)
    def __str__(self):
        return "{} ({})".format(nome_car, self.nome_model)

class parts(models.Model):
    name_car = models.ForeignKey(car, on_delete=models.PROTECT, related_name="parts")
    ns_parts = models.CharField(max_length=100) 

    def __str__(self):
        return self.nome_car.nome_car

manual2.py views

class PartsNewView(CreateView):
    template_name = ' Parts/parts.html' 
    model = parts
    def get_queryset(self):  
            return parts.objects.filter(name_car_id=self.kwargs['pk'])
    def get_success_url(self) -> str:
        messages.success(self.request, 'A parte do carro foi Cadastrado com sucesso')
        return reverse_lazy('car')
form1 
<form method="post">
 {% render_field form.name_car class="form-control" type="text" %}
 <button type="submit" class="btn btn-primary mr-2">Register</button>
</form> 
save.

in form2
form2
<form method="post">
 <!-- Doubt: what rule do I use to bring form1's car name to form2's field. -->  
 {% render_field form.name_car value="{{ object.name_car }}" class="form-control" type="text" %}
  
 <br>
 {% render_field form.parts class="form-control" type="text" %}  
 <button type="submit" class="btn btn-primary mr-2">Add</button>
</form>

1 answer

0

Subscribe the get and post methods either the View or Createview as well as using get_queryset and get_success_url, If there is a Foreignkey, you have to pass it to another form directly.

   class ItenABVIEW(View):
        template_name = 'superforms.html'
    
        def get(self, request):
            formulario1 = ItemAForms()
            formulario2 = ItemBForms()
            contexto = {'formulario1':formulario1,'formulario2':formulario2}
    
            return render(request, self.template_name, contexto)
    
        def post(self,request):
            formulario1 = ItemAForms(request.POST or None)
            formulario2 = ItemBForms(request.POST or None)
            # No caso é uma chave tera que pegar ela
            if formulario1.is_valid() and formulario2.is_valid() :
                chave = ItemA(itema=formulario1.data['itema'])
                chave.save()
                ItemB(itemb=formulario2.data['itemb'], itemdeFora_id=chave.pk).save()
                formulario1 = ItemAForms()
                formulario2 = ItemBForms()
    
            contexto = {'formulario1': formulario1, 'formulario2': formulario2}
    
    
            return render(request, self.template_name,contexto)

in html the two form in the same post:

<head>
    <meta charset="UTF-8">
    <title>A</title>
</head>
<body>


      <form  method="post">
          {% csrf_token %}
    {{ formulario1 }}
          {{ formulario2 }}
          

    <button type="submit">save</button>
      </form>
  • I didn’t understand your code. You joined the Rmulars ? In my case are separate Rmulars one at home template. but in madels.py with connected by a foreignkey. example: registration screen of the first Form1 where I put car name and model name and saved. After saving I click on this registered car and inside there is a "parts" button where you add the people of this car. When you click on the button appears a form form2 with field "name of the car" and "name of the piece" the field name of the car should already bring the name of the car. understands. ?

  • Use field lookups, a query in form2 that returns the car model’s name_car field. What I wrote was a single form that put the characteristics of the car and his piece on a single page, it’s not like that. uses lookups query can recover the name_car table value of model 1 but using query in model2,https://docs.djangoproject.com/en/3.2/topics/db/queries/#field-lookups

Browser other questions tagged

You are not signed in. Login or sign up in order to post.