我正在尝试通过父模型从子模型获取数据,我不知道是否有可能或有办法做到这一点,并且我想知道如何在这种情况下实现表单集概念,对于任何解决方案将不胜感激
models.py
class Client_Data(models.Model):
RC = models.CharField(max_length=50)
Raison_social = models.CharField(max_length=254)
NIF = models.CharField(max_length=50,unique=True)
AI = models.CharField(max_length=50,unique=True)
NIS = models.CharField(max_length=50,unique=True)
Banque = models.CharField(max_length=50,unique=True)
CB = models.CharField(max_length=50)
adresse = models.CharField(max_length=50)
slug = models.SlugField(blank=True, unique=True)
active = models.BooleanField(default=True)
class Contact(models.Model):
client = models.ForeignKey(Client_Data,blank=True,on_delete=models.CASCADE)
Nom = models.CharField(max_length=50)
post = models.CharField(max_length=50)
Tel = models.CharField(max_length=50)
email = models.EmailField(max_length=255,unique=True)
contact_type = models.CharField(default='Client_contact',max_length=50)
views.py
def save_client_form(request, form,Contact_form, template_name):
data = dict()
if request.method == 'POST':
if form.is_valid() and Contact_form.is_valid():
client = form.save()
contact = Contact_form.save(commit=False)
contact.client = client
contact.save()
form.save()
Contact_form.save()
data['form_is_valid'] = True
books = Client_Data.objects.all()
data['html_book_list'] = render_to_string('Client_Section/partial_client_c.html', {
'client': books
})
else:
print(form.errors)
print(Contact_form.errors)
data['form_is_valid'] = False
context = {'form': form,'contact_form':Contact_form}
data['html_form'] = render_to_string(template_name, context, request=request)
return JsonResponse(data)
def client_update(request,slug):
book = get_object_or_404(Client_Data, slug=slug)
contact = Contact.objects.select_related().filter(client=book.id)
print(contact)
if request.method == 'POST':
form = ClientForm(request.POST, instance=book)
contact_form = Contact_Form(request.POST, instance=contact)
else:
form = ClientForm(instance=book)
contact_form = Contact_Form(instance=contact)
return save_client_form(request, form,contact_form ,'Client_Section/partial_client_update.html')
答案 0 :(得分:1)
如果我对您的理解正确,那么您可以这样做:
contact = Contact.objects.select_related().filter(client=book.id)
addresse = contact.client.addresse
答案 1 :(得分:0)
在您view.py
from django.forms import inlineformset_factory
from django.shortcuts import render, get_object_or_404
def client_update(request, slug):
context = {}
book = get_object_or_404(Client_Data, slug=slug)
formset = inlineformset_factory(Client_Data, Contact, form=Contact_Form )
if request.method == 'POST':
form = ClientForm(request.POST, instance=book)
contactforms = formset(request.POST, prefix='contactformset', instance=book)
context['form'] = form
context['contactforms'] = contactforms
if contactforms.is_valid() and form.is_valid():
form.save()
contactforms.save()
return HttpResponse("your data saved")
else:
return render(request, 'Client_Section/partial_client_update.html', context)
else:
form = ClientForm(instance=book)
contactforms = formset(prefix='contactformset', instance=book)
context['form'] = form
context['contactforms'] = contactforms
return render(request, 'Client_Section/partial_client_update.html', context)
partial_client_update.html
<form method="POST">
{% csrf_token %}
{{form}}
<hr>
{% formset in contactforms %}
{{formset }}
<hr>
{% endfor %}
<button type="submit ">update</button>
</form>