尝试从窗体中保存ManyToMany值时出现Django ValueError

时间:2015-03-13 02:27:49

标签: django django-models django-forms manytomanyfield

我收到错误""需要为字段"数据集"在此之前可以使用多对多关系。"尝试将值分配给视图中的ManyToMany字段时。我在这里看了许多相关的问题,说我必须首先保存我的数据集对象。我想我在做......出了什么问题?我的数据库已包含四个主题项。

models.py

class Subject(TimeStampedModel):
    subject_type = models.CharField(max_length=128, blank=False)

    def __unicode__(self):
        return self.subject_type

class Dataset(TimeStampedModel):
    dataset_id = models.CharField(max_length=256)    
    dataset_doi = models.CharField(max_length=15)       
    dataset_name = models.CharField(max_length=256, blank=False)
    dataset_description = models.TextField(blank=False)
    lab = models.CharField(max_length=256, blank=False)
    biological_sample = models.CharField(max_length=256, blank=False)
    subject_type = models.ManyToManyField('Subject', related_name='datasets', blank=True)
    date_collected = models.DateField(blank=True)  

    collection_facility = models.ManyToManyField('CollectionFacility', related_name='datasets', blank=True)
    processing_notes = models.TextField(blank=True)

    release_date = models.DateField()   
    release_asap = models.BooleanField()    
    pdb_code = models.CharField(max_length=256, blank=True)     
    publication_link = models.URLField(blank=True)      

    def create_name(self):
        self.dataset_name = "%s %s" % (self.biological_sample, self.lab)

    def save(self, *args, **kwargs):
        self.dataset_id = self.id

    def __unicode__(self):
        return "%s : %s" % (self.dataset_name, self.dataset_id)

forms.py RegistrationForm:

class RegistrationForm(forms.Form):

    subject_type = forms.ModelMultipleChoiceField(
        label="Subject",
        queryset = Subject.objects.all(),
        widget=forms.CheckboxSelectMultiple(),  
        required = True,
    )

views.py

def create_registration(form):
    dataset = Dataset()
    dataset.DOI = "preUpload"
    dataset.lab = form.cleaned_data['lab']
    dataset.biological_sample = form.cleaned_data['sample']
    dataset.resource_type = form.cleaned_data['dataset_type']
    dataset.dataset_description = form.cleaned_data['dataset_description']

    dataset.date_collected = form.cleaned_data['date_collected']
    dataset.release_date = form.cleaned_data['release_date']
    dataset.release_asap = form.cleaned_data['release_asap']

    if form.cleaned_data['pdb_code']:
        dataset.pdb_code = form.cleaned_data['pdb_code']

    if form.cleaned_data['publication_link']:
        dataset.publication_link = form.cleaned_data['publication_link']

    dataset.create_name()

    dataset.save()    # I don't think this save is working?

    subjects = form.cleaned_data['subject_type']
    dataset.subject_type = [x for x in subjects]

    for facility in form.cleaned_data['facility']
        dataset.collection_facility.add(facility)

    dataset.save()

    return dataset


def registration_submit(request):
    registration_form = RegistrationForm(request.POST)
    if registration_form.is_valid():
        registration = create_registration(registration_form)
.......

1 个答案:

答案 0 :(得分:1)

您忘记在覆盖save()方法中调用原始Dataset.save()

def save(self, *args, **kwargs):
    self.dataset_id = self.id
    super(Dataset, self).save(*args, **kwargs)