我有一个邮政编码表和一个人员表。输入人员 zip 时,我需要用相关 zip 表中的城市填充人员城市字段。
编辑:我开始相信这是 Django 无法完成的事情。但可以使用 javascript 来完成。我对此的了解甚至比姜戈还要少。有人吗?
models.py:
from django.db import models
class Zip(models.Model):
zipcode = models.IntegerField()
city = models.CharField(max_length=200)
def __str__(self):
return str(self.zipcode)
class Person(models.Model):
name = models.CharField(max_length=200)
zipcode = models.ForeignKey('Zip',on_delete=models.CASCADE,related_name="zip_of_this_person")
personcity = models.CharField(max_length=200)
def __str__(self):
return self.name
表格.py
from django import forms
from .models import Zip, Person
class ZipForm(forms.ModelForm):
class Meta:
model = Zip
fields = ('zipcode', 'city',)
class PersonForm(forms.ModelForm):
class Meta:
model = Person
fields = ('name', 'zipcode', 'personcity' )
views.py
from django.shortcuts import render, redirect, get_object_or_404
from .forms import PersonForm, ZipForm
from .models import Zip, Person
# Create your views here.
def index(request):
return render(request, 'theapp/index.html' )
def person_new(request):
if request.method == "POST":
form = PersonForm(request.POST)
# author_name = '2222'.Zip.city
if form.is_valid():
post = form.save(commit=False)
post.save()
return redirect('person_detail', pk=post.pk)
else:
form = PersonForm()
return render(request, 'theapp/person.html', {'form': form})
def zip_new(request):
if request.method == "POST":
form = ZipForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.save()
return redirect('zip_detail', pk=post.pk)
else:
form = ZipForm()
return render(request, 'theapp/zip.html', {'form': form})
def person_detail(request, pk):
post = get_object_or_404(Person, pk=pk)
weather= "testing purposes"
context= {
'post': post,
'weather': weather,
}
return render(request, 'theapp/person_detail.html', context)
def zip_detail(request, pk):
post = get_object_or_404(Zip, pk=pk)
return render(request, 'theapp/zip_detail.html', {'post': post})
最后是 html:
{% extends 'theapp/base/base.html' %}
{% load static %}
{% block title %}Tittel Tab{% endblock %}
{% block site_css %}{% endblock %}
{% block main_heading %}{% endblock %}
{% block header_content %}{% endblock %}
{% block body %}
<section>
<h2>Person</h2>
</section>
<article class="post">
<h2>Name:{{ post.name }}</h2>
<p>Zip/city:{{ post.zipcode }}{{ post.personcity }}</p>
{{weather}}
</article>
{% endblock %}
所以我一直在寻找,但没有真正找到任何对我有帮助的东西。我无法找到任何解决方案。如果是相反的方式显示所有相关的 ti zip,那就很容易了。我意识到这可能是一个前端问题,也许不完全是 Django 的问题。但使用关系数据库仍然是非常基本的。任何帮助都表示感谢。
您可以在personcity上允许空和空白,然后在保存模型时填充它; 为此,您可以采用简单的方法并重写 .save() 方法 像这样的事情:
class Person(models.Model):
name = models.CharField(max_length=200)
zipcode = models.ForeignKey('Zip',on_delete=models.CASCADE,related_name="zip_of_this_person")
personcity = models.CharField(max_length=200, null=True, blank=True)
def __str__(self):
return self.name
def save(self, *args, **kwargs):
if not self.personcity and self.zipcode and self.zipcode.city:
self.personcity = self.zipcode.personcity
super(Message, self).save(*args, **kwargs)
或者你可以使用 pre_save 信号
来做到这一点