django根据模型属性自动填充一些数据 [英] django auto filling some data based on model attribute
问题描述
正在开发一种在保存模型时想要捕获某些信息的概念。要了解全貌,我有一个应用程序核心与以下型号
core / models.py
<$ p从django.db导入模型$ p code从django.contrib.auth.models导入
来自django.contrib.contenttypes.models的用户
从django导入ContentType
。 contrib.contenttypes进口通用
从transmeta import TransMeta
从django.utils.translation import ugettext_lazy为_
导入信号
类审计(models.Model):
## TODO:Document
#通过DJANGO内容类型使用泛型关系的多态模型
operation = models.CharField(_('Operation'),max_length = 40 )
operation_at = models.DateTimeField(_('Operation At'),auto_now_add = True)
operation_by = models.ForeignKey(User,null = True,blank = True,related_name =%(app_label) s _%(class)s_y +)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
cont ent_object = generic.GenericForeignKey('content_type','object_id')
审计模型是一般内容类型,目前正在其他应用程序中附加,例如博客
blog / models.py
从django.db导入模型
从django.contrib.contenttypes导入通用
从django.contrib.contenttypes.models导入ContentType
从django.template.defaultfilters导入slugify
from django.utils.translation import ugettext_lazy as _
#在这里创建你的模型。
class Meta:
verbose_name_plural =Categories
类文章(models.Model):
title = models.CharField(max_length = 100)
slug = models.SlugField(editable = False,unique_for_year = True)
content = models.TextField()
is_active = models.BooleanField()
published_at = models.DateTimeField('Publish at',auto_now = True)
related_articles = models.ManyToManyField('self',null = True,blank = True)
audit_obj = generic.GenericRelation(' core.Audit',editable = False,null = True,blank = True
我的第一次尝试是,我做了一个post_save信号,其中我正在检查实例是否传递包含audit_obj属性,然后使用 article.audit_obj.create()。save()
保存记录。
不幸的是,这并不完全适用于我,因为我无法传递请求,也不能访问请求来检索用户信息。
所以,我正在考虑创建一个自定义信号,并重写form_save方法(如果有这样的事情),然后使用参数传递请求对象以及模型对象。
有关如何做到这一点的任何建议?
问候,
编辑(2011年1月20日):
感谢@Yuji为您的时间。那么,我想要实现的是尽可能地保持我的代码为干。我最终要做的是,每次创建新的模型时,我只会创建一个附加属性并将其命名为audit_obj,我将创建一个单独的代码,一个信号,或者覆盖django内核本身的save方法。代码段将始终检查是否存在具有以下名称的属性,因此在aduti表中创建一个记录。
我将在我的模型类或管理器中创建一个函数,并从我的表单中保存(无论你在哪里)
class AuditManager(models.Manager):
def save_from_object(self,request,obj):
audit = Audit()
audit.object_id = obj.id
audit.operation_by = request.user
#...
audit.save()
类审核(models.Model):
## TODO:Document
#使用通用关系通过DJANGO内容类型的多形态模型
operation = models。 CharField(_('Operation'),max_length = 40)
operation_at = models.DateTimeField(_('Operation At'),auto_now_add = True)
operation_by = models.ForeignKey(User,null = True ,blank = True,related_name =%(app_label)s _%(class)s_y +)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_objec t = generic.GenericForeignKey('content_type','object_id')
objects = AuditManager()
class MyBlogForm(forms.ModelForm):
class Meta:
model = article#btw我将使用资本信件为类
def save(self,* args,** kwargs):
super(MyBlogForm, self).save(* args,** kwargs)
Audit.objects.save_from_object(request,self.instance)
am working on a concept in which I want to capture certain information when a model is saved. To understand the full picture, I have a app core with the following model
core/models.py
from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from transmeta import TransMeta
from django.utils.translation import ugettext_lazy as _
import signals
class Audit(models.Model):
## TODO: Document
# Polymorphic model using generic relation through DJANGO content type
operation = models.CharField(_('Operation'), max_length=40)
operation_at = models.DateTimeField(_('Operation At'), auto_now_add=True)
operation_by = models.ForeignKey(User, null=True, blank=True, related_name="%(app_label)s_%(class)s_y+")
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
Audit model is a generic content type, am currently attaching it with other apps such as in blog
blog/models.py
from django.db import models
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from django.template.defaultfilters import slugify
from django.utils.translation import ugettext_lazy as _
# Create your models here.
class Meta:
verbose_name_plural = "Categories"
class article(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(editable=False, unique_for_year=True)
content = models.TextField()
is_active = models.BooleanField()
published_at = models.DateTimeField('Publish at',auto_now=True)
related_articles = models.ManyToManyField('self', null=True, blank=True)
audit_obj = generic.GenericRelation('core.Audit', editable=False, null=True, blank=True)
My first attempt was, I made a post_save signal in which I was checking if the instance passed containing audit_obj attribute and then saving a record in using article.audit_obj.create().save()
.
Unfortunately, this did not entirely work out for me since I cannot pass the request nor I can access the request to retrieve the user information.
So, I was thinking to create a custom signal and override the form_save method (if there is such a thing) and then using arguments to pass the request object as well as the model object.
Any advice on how I can do that?
Regards,
EDIT (20th of Jan, 2011):
Thanks @Yuji for your time. Well, what am trying to achieve is to keep my code as DRY as possible. What I want to do ultimately, every time I create new model, I will only create an additional attribute and name it audit_obj and I will create a single piece of code, either a signal or to override the save method inside the django core itself. The peiece of code will always check if an attribute with the following name exists and therefore creates a record in aduti table.
I'd just create a function in my model class or Manager and call it from my form save (wherever yours might be)
class AuditManager(models.Manager):
def save_from_object(self, request, obj):
audit = Audit()
audit.object_id = obj.id
audit.operation_by = request.user
# ...
audit.save()
class Audit(models.Model):
## TODO: Document
# Polymorphic model using generic relation through DJANGO content type
operation = models.CharField(_('Operation'), max_length=40)
operation_at = models.DateTimeField(_('Operation At'), auto_now_add=True)
operation_by = models.ForeignKey(User, null=True, blank=True, related_name="%(app_label)s_%(class)s_y+")
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
objects = AuditManager()
class MyBlogForm(forms.ModelForm):
class Meta:
model = article # btw I'd use Capital Letters For Classes
def save(self, *args, **kwargs):
super(MyBlogForm, self).save(*args, **kwargs)
Audit.objects.save_from_object(request, self.instance)
这篇关于django根据模型属性自动填充一些数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!