我有这个简单的模型,就像一个rsync配置,用于预先填写芹菜周期性任务的字段。我第一次通过模型创建一个新的rsync配置一切正常,并且正在创建一个没有问题的新周期性任务。当我尝试更改某些将改变任务参数等任务字段的字段时,我得到的“IntegrityError列名称并不是唯一的”我觉得它与模型保存方法有关但我不知道如何修正它。谁有想法?
这是模型:
from django.forms import ModelForm
from djcelery.models import IntervalSchedule
from djcelery.models import PeriodicTask, IntervalSchedule
INTERVAL=(
('every=5','period 5 minutes'),
)
class Customer(models.Model):
"""(Customer description)"""
customername = models.CharField(blank=True, max_length=30)
emailaddress = models.EmailField()
phonenumber = models.CharField(blank=True, max_length=10)
class Meta:
verbose_name_plural = "Customer"
def __unicode__(self):
return self.customername
class RsyncConfig(models.Model):
"""(RsyncConfig description)"""
cname = models.ForeignKey(Customer)
rsyncname = models.CharField(blank=True, max_length=255)
interval=models.CharField(max_length=8,choices=INTERVAL)
fromip = models.IPAddressField(blank=True)
source_dir = models.CharField(blank=True, max_length=255)
destination_dir = models.CharField(blank=True, max_length=255)
rsync_args = models.CharField(blank=True, max_length=255)
class Meta:
verbose_name_plural = "Rsync Config"
def __unicode__(self):
return self.cname.customername
这是admin.py表单。
from django.contrib import admin
from django import forms
from djcelery.models import PeriodicTask, IntervalSchedule
from newrsync.models import Customer,RsyncConfig
class CustomerAdmin(admin.ModelAdmin):
class Meta:
model = Customer
class RsyncConfigAdminForm(forms.ModelForm):
list_display = ('customername', 'rsyncname','source_dir','destination_dir')
class Meta:
model = RsyncConfig
def __init__(self, *args, **kwargs):
super(RsyncConfigAdminForm,self).__init__(*args, **kwargs)
def save(self, commit=True):
interval = IntervalSchedule.objects.get(every=5,period="minutes")
model = super(RsyncConfigAdminForm, self).save(commit = False)
model.cname = self.cleaned_data['cname']
model.rsyncname = self.cleaned_data['rsyncname']
model.fromip = self.cleaned_data['fromip']
model.source_dir = self.cleaned_data['source_dir']
model.destination_dir = self.cleaned_data['destination_dir']
model.rsync_args = self.cleaned_data['rsync_args']
if commit:
model.save()
PeriodicTask.objects.get_or_create(
interval=interval,
task='apps.mftconfig.tasks.rsync_proc',
args=['rsync',
model.rsync_args,
model.source_dir,
model.destination_dir],
kwargs = {},
name = (model.cname," ",model.rsyncname),
enabled=False
)
return model
class RsyncConfigAdmin(admin.ModelAdmin):
form = RsyncConfigAdminForm
list_display = ('cname', 'rsyncname','source_dir','destination_dir')
admin.site.register(Customer,CustomerAdmin)
admin.site.register(RsyncConfig,RsyncConfigAdmin)
答案 0 :(得分:0)
我基本上在保存新版本之前就删除了该对象。它不完美但至少我绕过了PeriodicTask模型中的唯一限制,现在让我们希望它不会在以后咬我的屁股上。 如果有人有任何建议,请! ; - )
class RsyncConfigAdminForm(forms.ModelForm):
list_display = ('customername','rsyncname','source_dir','destination_dir')
class Meta:
model = RsyncConfig
def __init__(self, *args, **kwargs):
super(RsyncConfigAdminForm,self).__init__(*args, **kwargs)
def save(self, commit=True):
instance = super(RsyncConfigAdminForm, self).save(commit = False)
instance.customername = self.cleaned_data['customername']
instance.rsyncname = self.cleaned_data['rsyncname']
instance.fromip = self.cleaned_data['fromip']
instance.source_dir = self.cleaned_data['source_dir']
instance.destination_dir = self.cleaned_data['destination_dir']
instance.rsync_args = self.cleaned_data['rsync_args']
interval = IntervalSchedule.objects.get(every=5,period="minutes")
p=PeriodicTask.objects.filter(name=instance.rsyncname)
p.delete()
PeriodicTask.objects.get_or_create(
interval=interval,
task='apps.mftconfig.tasks.rsync_proc',
args=['rsync',
instance.rsync_args,
instance.source_dir,
instance.destination_dir],
kwargs = {},
name = (instance.rsyncname),
enabled=True
)
if commit:
instance.save()
return instance