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>
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. ?
– Leticia Lima
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
– stack.cardoso