我目前正在 django 中构建一个页面,其中有 4 个表单字段、2 个文本、2 个选择字段,当提交时,它会获取这些字段并搜索多个模型以查找匹配项。
模型如下所示:
class Person(models.Model):
user = models.ForeignKey(User, blank=True, null=True, verbose_name="the user associated with this profile")
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
about = models.TextField(max_length=255, blank=True, null=True)
birthdate = models.DateField(blank=True, null=True, verbose_name="Birthdate (yyyy-mm-dd)")
GENDER_CHOICES = (
(u'M', u'Male'),
(u'F', u'Female'),
)
gender = models.CharField(max_length=1, choices = GENDER_CHOICES, default = 'M')
picture = models.ImageField(upload_to='profile', blank=True, null=True)
nationality = CountryField(blank=True, null=True)
location = models.CharField(max_length=255, blank=True, null=True)
command_cert = models.BooleanField(verbose_name="COMMAND certification")
experience = models.ManyToManyField('userProfile.MartialArt', blank=True, null=True)
我正在尝试搜索 first_name 字段、last_name 字段、nationality 字段和 experience 字段,但是如果 first_name 字段为空,我需要传递一个空值,以便它返回所有行,然后从那里过滤姓氏以同样的方式,由于某种原因,它对我来说根本不起作用。这是我的sqs:
results = SearchQuerySet().models(Person).filter(first_name=sname, last_name=slastname, nationality=scountry, experience__pk=sexperience)
有任何想法吗?