我将使用https://github.com/ghdpro/animesuki/tree/master/animesuki/history并在自己的权限下对其进行修改,但我停在某一点,我无法用自己的技能跳起来..
问题是,如果我是超级用户,或者我具有此模型中给出的权限,它将添加到我的主模型Movie记录中;如果没有权限,则将其添加到ChangeRequest中,我的问题是为此,在将批准添加到主Movie模型中之后,并在接受和更改字段时更新给定对象中的数据。 它或多或少可以根据我的需要工作,但是我无法跳过这一步..
我不需要整个解决方案,只需要技巧或一些我可以从中学习如何做的源代码
编辑:
class Movie(HistoryModel):
title = models.CharField(max_length=200, unique=True)
slug = extension_fields.AutoSlugField(populate_from='title', blank=True)
description = models.TextField(max_length=2000, default='', blank=True)
picture = ImageField(upload_to=user_directory_path, default='img/movie/default/brak_obrazka.jpg', blank=True)
pub_date = models.DateField(blank=True, null=True)
mpaa = models.CharField(max_length=5, choices=AGE_GROUP, default='PG-13')
movie_time = models.CharField(max_length=20, default='90')
我的问题是,当前正常添加电影的用户仅在从Pending = Approved更改后,如何添加到上述给定链接中的ChangeRequest模型中,如何将此模型添加到电影中,然后是否添加更改以更新给定模型
答案 0 :(得分:0)
我没有看到完整的代码,但我认为您应该看起来HistoryModel
您可以看到here,HistoryModel
具有save()
和save_related()
方法。
您应该在这里看到以了解其下发生的事情。
def save(self, *args, **kwargs):
self._cr = self.create_changerequest()
if self.self_approve and self.sanity_checks and self.sanity_checks_extra:
# Approve immediately if right conditions are met
self._cr.status = ChangeRequest.Status.APPROVED
self._cr.save()
# Save actual model instance if: ChangeRequest object was saved -and- the request was self-approved
# (ChangeRequest object will not have been saved if data was not altered)
if self._cr.pk:
self.log()
if self._cr.status == ChangeRequest.Status.APPROVED:
super().save(*args, **kwargs)
self._cr.set_object(self)
self._cr.save()
# Generate message
verb = {ChangeRequest.Type.ADD: 'Added',
ChangeRequest.Type.MODIFY: 'Updated'} # DELETE or RELATED requests shouldn't be handled by save()
self.add_message(messages.SUCCESS, verb[self._cr.request_type], self._cr.object_type, self._cr.object_str)
elif self._cr.status == ChangeRequest.Status.PENDING:
self.add_message_pending()
# Reset cached property
if hasattr(self, 'has_pending'):
delattr(self, 'has_pending')
def save_related(self, formset):
self._cr = self.create_changerequest(request_type=ChangeRequest.Type.RELATED)
self._cr.related_type = ContentType.objects.get_for_model(formset.model)
self._cr.data_revert = formset_data_revert(formset)
self._cr.data_changed = formset_data_changed(formset)
if self.self_approve and self.sanity_checks and self.sanity_checks_extra:
# Approve immediately if right conditions are met
self._cr.status = ChangeRequest.Status.APPROVED
self._cr.save()
# Save formset if: ChangeRequest object was saved -and- the request was self-approved
# (ChangeRequest object will not have been saved if data was not altered)
if self._cr.pk:
self.log()
if self._cr.status == ChangeRequest.Status.APPROVED:
formset.save()
# Generate message(s)
for obj in formset.new_objects:
self.add_message(messages.SUCCESS, 'Added', self._cr.related_type, obj)
self.log('Add', format_object_str(self._cr.related_type, obj, obj.pk))
for obj in formset.changed_objects:
self.add_message(messages.SUCCESS, 'Updated', self._cr.related_type, obj)
self.log('Modify', format_object_str(self._cr.related_type, obj, obj.pk))
for obj in formset.deleted_objects:
self.add_message(messages.SUCCESS, 'Deleted', self._cr.related_type, obj)
self.log('Delete', format_object_str(self._cr.related_type, obj, obj.pk))
# Refresh data_changed: any new instances should now have a pk set
self._cr.data_changed = formset_data_revert(formset)
self._cr.save()
elif self._cr.status == ChangeRequest.Status.PENDING:
self.add_message_pending()
# Reset cached property
if hasattr(self, 'has_pending'):
delattr(self, 'has_pending')