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

标签 python django

我正在尝试检查字段值是否已被删除,如果所有字段都包含值,则返回FalseTrue

我的代码实际上不检查字段中的值,而只是检查字段是否存在。

如何查看每个特定字段的值?

这是我的模型,在我看来,我必须检查该功能。

# 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)

最佳答案

从查询集中获取,这将是一个dict对象。

解决方案:

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

关于python - 如何在Python/Django中检查字段的值是否已被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46769951/

相关文章:

python - 表格单元格内的文本未正确对齐

Django 中的 Python 日志记录

python - 使用 MatplotLib python 的交互式堆积条形图

python - 比较 Boost.Odeint 与 Scipy.integrate.odeint?

javascript - Django - 将模型序列化为 json 以用作 JavaScript 对象

javascript - 使用 Python (Django)、HTML、CSS、Javascript 的响应式导航栏

django - 如何记住 QuerySet.update() 之后要处理的对象

python - Pyhive、SASL 和 Python 3.5

python - DBSCAN 从图中去除噪声

python - 在 tensorflow 中创建张量数组?