我想编写一个视图,将某些模型字段值重置为默认值。如何获取默认模型字段值?
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"main" bundle:nil];
UIViewController *loginViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"loginViewController"];
UIViewController *lastPresentedViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
while (lastPresentedViewController.presentedViewController) {
lastPresentedViewController = lastPresentedViewController.presentedViewController;
}
[lastPresentedViewController presentViewController:loginViewController animated:true completion:nil];
所以我想要的是:
class Foo(models.Model):
bar_field = models.CharField(blank=True, default='bar')
答案 0 :(得分:3)
自Django 1.10起:myfield = Foo._meta.get_field('bar_field').get_default()
请参阅此处(文档中未详细说明......):https://github.com/django/django/blob/master/django/db/models/fields/init.py#L2301
答案 1 :(得分:0)
myfield = Foo._meta.get_field_by_name('bar_field')
,默认只是字段的一个属性:
myfield.default