django 模型 _meta.get_fields() 只是日期时间字段
django model _meta.get_fields() just datettime fields
我只想收集字段名类型是日期时间
_meta.get_fields()
Model.py :
class testmodel (models.Model):
name = models.CharField(max_length=200)
field = models.CharField('field', max_length=50, choices=(), default=None)
create_time = models.DateTimeField(null=True,blank=True)
complete_time = models.DateTimeField(null=True,blank=True)
admin.py :
all_fields = [f.name for f in testmodel._meta.fields]
class myform(forms.ModelForm):
field = forms.ChoiceField(choices=all_fields)
name = forms.TextInput()
class MyModelAdmin(admin.ModelAdmin):
fields = ('name', 'field',)
list_display = ('name', 'field',)
form = myform
admin.site.register(testmodel,MyModelAdmin)
有什么解决方案?
使用列表理解作为
from django.db.models import DateTimeField
datetime_model_fields = [field for field in MyModelClass._meta.get_fields() if isinstance(field, DateTimeField)]
我只想收集字段名类型是日期时间
_meta.get_fields()
Model.py :
class testmodel (models.Model):
name = models.CharField(max_length=200)
field = models.CharField('field', max_length=50, choices=(), default=None)
create_time = models.DateTimeField(null=True,blank=True)
complete_time = models.DateTimeField(null=True,blank=True)
admin.py :
all_fields = [f.name for f in testmodel._meta.fields]
class myform(forms.ModelForm):
field = forms.ChoiceField(choices=all_fields)
name = forms.TextInput()
class MyModelAdmin(admin.ModelAdmin):
fields = ('name', 'field',)
list_display = ('name', 'field',)
form = myform
admin.site.register(testmodel,MyModelAdmin)
有什么解决方案?
使用列表理解作为
from django.db.models import DateTimeField
datetime_model_fields = [field for field in MyModelClass._meta.get_fields() if isinstance(field, DateTimeField)]