How to work with unsaved many-to-many relations in django?

前端 未结 3 1166
孤街浪徒
孤街浪徒 2021-01-12 23:51

I have a couple of models in django which are connected many-to-many. I want to create instances of these models in memory, present them to the user (via custom met

3条回答
  •  伪装坚强ぢ
    2021-01-13 00:04

    I think that using django forms may be the answer, as outlined in this documentation (search for m2m...).

    Edited to add some explanation for other people who might have the same problem:

    say you have a model like this:

    from django.db import models
    from django.forms import ModelForm
    
    class Foo(models.Model):
        name = models.CharField(max_length = 30)
    
    class Bar(models.Model):
          foos = models.ManyToManyField(Foo)
    
      def __unicode__(self):
          return " ".join([x.name for x in foos])
    

    then you cannot call unicode() on an unsaved Bar object. If you do want to print things out before they will be saved, you have to do this:

    class BarForm(ModelForm):
        class Meta:
            model = Bar
    
    def example():      
        f1 = Foo(name = 'sue')
        f1.save()
        f2 = foo(name = 'wendy')
        f2.save()
        bf = BarForm({'foos' : [f1.id, f2.id]})
        b = bf.save(commit = false)
        # unfortunately, unicode(b) doesn't work before it is saved properly,
        # so we need to do it this way: 
        if(not bf.is_valid()):
            print bf.errors
        else:
            for (key, value) in bf.cleaned_data.items():
                print key + " => " + str(value)
    

    So, in this case, you have to have saved Foo objects (which you might validate before saving those, using their own form), and before saving the models with many to many keys, you can validate those as well. All without the need to save data too early and mess up the database or dealing with transactions...

提交回复
热议问题