我有一个模特
class Project(models.Model):
STATUS_CHOICE= (('going_on','GOING_ON'),
('project_co','PROJECT_COMPLETED'),
('contract_c','CONTRACT_COMPLETED'),
('contract_e','CONTRACT_EXTENDED'),
)
title= models.CharField(max_length=30,unique=True)
description= models.CharField(max_length=2000,null=True,blank=True)
assigned_date= models.DateField(null=True, blank=True)
completion_date= models.DateField(null=True, blank=True)
join_date= models.DateField(null=True, blank=True)
technology= models.ForeignKey(Technology,null=True, blank=True)
consultant= models.ForeignKey(User, on_delete=models.CASCADE, related_name= 'project')
status= models.CharField(max_length= 10, choices= STATUS_CHOICE, default= 'pending')
file=models.FileField(upload_to=get_attachment_file_path,null=True,blank=True)
history = HistoricalRecords()
现在我想跟踪这个项目模型中的所有变化。
但它提供的功能有限。我知道我可以在Django中使用信号。但是我必须以这样一种方式发送历史数据:特定user
与该项目的STATUS_CHOICE
一起完成了多少项目。
答案 0 :(得分:2)
我认为这可以解决。它应该是相当有效的,即使你有数百万行。
<强> models.py 强>
from django.utils import timezone
class Change(models.Model):
project = models.ForeignKey(Project)
changed_field = models.CharField("field_name")
changed_data = models.TextField() # you can improve this by storing the data in compressed format
chaged_at = models.DateTimeField(default=timezone.now)
status = models.CharField(max_length= 10, choices= STATUS_CHOICE, default= 'pending')
<强> serializers.py 强>
class ChangeSerializer(serializers.ModelSerializer):
class Meta:
model = Change
fields = ("project","changed_field",# all the other fields that you wanna add)
<强> views.py 强>
def get_changes(request,project_id,status):
if request.method == 'GET':
changes = Change.objects.filter(status=status, project=Project.objects.get(pk=project_id))
serializer = ChangeSerializer(changes, many=True)
return JSONResponse(serializer.data)
<强> urls.py 强>
urlpatterns = [
url(r'changes/(?P<project_id>\d+)/(?P<status>[a-z]+)/',views.get_changes)
]
如果您需要任何其他更改,请告诉我