2012-02-28 45 views
0

我有這個簡單的模型,就像一個rsync配置,用於預填充芹菜週期性任務的字段。我第一次通過模型創建一個新的rsync配置一切正常,一個新的週期性任務正在創建,沒有問題。當我嘗試和改變某些領域,將改變任務領域,如任務參數,我得到一個「IntegrityError列名稱不是唯一的」我覺得它與模型保存方法有關,但我不知道如何修正它。任何人有一些想法?Django-celery 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)修復save()中的縮進和1)在django shell中重現問題並粘貼shell會話+追蹤。 – jpic 2012-02-28 10:19:04

+0

嗨jpic,身份似乎很好?我不知道你的意思是在shell中重現問題。這是一種形式,我不確定如何在沒有形式的情況下重現它? – Daxomatic 2012-02-28 10:28:18

+0

嘗試重現它沒有形式。嘗試使用表格RsyncConfigAdminForm(data = {...}) – jpic 2012-02-28 10:41:25

回答

0

我基本上結束了做一個刪除對象的權利之前,我保存了新的version.It並不完美,但至少我規避了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