简体   繁体   English

如何在 Python/Django 中检查字段的值是否已被删除

[英]How to check if the value of a field has been deleted in Python/ Django

I'm trying to check if a value of field has been deleted and then return False or True if all the fields contain values.我正在尝试检查字段的值是否已被删除,然后如果所有字段都包含值,则返回FalseTrue

My code actually doesn't check the value in the fields but just checks if the fields exists.我的代码实际上不检查字段中的值,而只是检查字段是否存在。

How to check the values of every specific field?如何检查每个特定字段的值?

This is my models and in my views I have to check the function.这是我的模型,在我看来,我必须检查功能。

# models.py
class CompanyDirector(models.Model):
    username = models.CharField(max_length=128, blank=True, null=True)
    directorTitle = models.CharField(max_length=8, blank=True, null=True)
    directorName = models.CharField(max_length=64, blank=True, null=True)
    directorSurname = models.CharField(max_length=64, blank=True, null=True)
    directorId = models.CharField(max_length=16, blank=True, null=True)  
    proffessionStartDate = models.DateTimeField(blank=True, null=True)

# views.py
ret = {'complete': False}
  try:
      company_director = 
 CompanyDirector.objects.filter(company__token=token).first()
      if company_director:
        ret['complete'] = True
        for field in company_director._meta.get_all_field_names():
          if not getattr(company_director, field):
            ret['complete'] = False
            break;
  except ValueError as e:
     print (e)
  return Response(ret)

Get the values from the queryset, which will be a dict object.从查询集中获取values ,这将是一个dict对象。

Solution :解决方案

company_director = CompanyDirector.objects.filter(company__token=token).values().first()
if company_director:
    for field, value in company_director.items():
        if value is None:
            return False
    return True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM