Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在model save()中,如何获取以';foo';_Python_Django_Django Models - Fatal编程技术网

Python 在model save()中,如何获取以';foo';

Python 在model save()中,如何获取以';foo';,python,django,django-models,Python,Django,Django Models,我有一个django模型: from django.db import models class MyModel(models.Model): foo_it = model.CharField(max_length=100) foo_en = model.CharField(max_length=100) def save(self): print_all_field_starting_with('foo_') super(MyMode

我有一个django模型:

from django.db import models

class MyModel(models.Model):
    foo_it = model.CharField(max_length=100)
    foo_en = model.CharField(max_length=100)

    def save(self):
        print_all_field_starting_with('foo_')
        super(MyModel, self).save()
所以我想让所有字段都从foo开始(作为一个例子),并用它做一些事情。 我不能在代码中这样做,因为我不知道模型中的所有字段(我使用的是django transmeta)

那么,我该怎么做呢

提前感谢;)

您可以执行以下操作:

for field in dir(self):
    if field.startswith('foo_'):
      # getting with getattr(self, field)
      # setting with setattr(self, field, value)
如果要获取字段列表,还可以执行以下操作:

foo_fields = [field for field in dir(self) if field.startswith('foo_')]
或打印
foo
字段的值列表:

print map(lambda x: getattr(self, x), [field for field in dir(self) if field.startswith('foo_')])
你可以做:

for field in dir(self):
    if field.startswith('foo_'):
      # getting with getattr(self, field)
      # setting with setattr(self, field, value)
如果要获取字段列表,还可以执行以下操作:

foo_fields = [field for field in dir(self) if field.startswith('foo_')]
或打印
foo
字段的值列表:

print map(lambda x: getattr(self, x), [field for field in dir(self) if field.startswith('foo_')])

这样就可以了,不过您还需要传入要打印其字段的对象:

import inspect
def print_all_field_starting_with(prefix, object):
    for name, value in inspect.getmembers(object):
        if name.startswith(prefix):
            print name # or do something else

有关更多信息,请参见。

这将完成此操作,不过您还需要传入要打印其字段的对象:

import inspect
def print_all_field_starting_with(prefix, object):
    for name, value in inspect.getmembers(object):
        if name.startswith(prefix):
            print name # or do something else

有关更多信息,请参见。

有一个
get_all_field_names()
方法,该方法内置于所有模型的
Meta
子类中,可以在
foo中找到。
get_all_field_names()

所以这将是一件简单的事情:

def print_all_fields_starting_with(obj, starter):
    fields = [x for x in obj._meta.get_all_field_names() if x.startswith(starter)]
    for field in fields:
        print getattr(obj, field)
在自定义的
save()
中:


有一个
get\u all\u field\u names()
方法,该方法内置于所有模型的
Meta
子类中,可以在
foo中找到

所以这将是一件简单的事情:

def print_all_fields_starting_with(obj, starter):
    fields = [x for x in obj._meta.get_all_field_names() if x.startswith(starter)]
    for field in fields:
        print getattr(obj, field)
在自定义的
save()
中:


不,它不起作用:(我收到一个错误:Manager无法通过Post实例访问。但是,谢谢。看起来有一个Django票证与此错误相关:不,它不起作用:(我收到一个错误:Manager无法通过Post实例访问。但是,谢谢。看起来有一个Django票证与此错误相关:酷!很好的解决方案:D谢谢;)酷!很好的解决方案:D谢谢;)