Thursday, November 24, 2011

Re: Help with many-to-many field update

Hey Leslie,


Cheers,

On 25 November 2011 00:43, Nichehosters <lesliem@mygolf.za.net> wrote:
Hi,
I am relatively new to Django, and am trying to update a many-to-many
related table using a ModelForm.
I see fro the documentation that if I use save(commit=False) - which I
do - I need to use save_m2m() after saving the initial form.  I am
using the save_m2m(), but my related table does not get updated.
Any help appreciated.
I am using django V1.3.1 on linux using Postgresql as the db.

Details:

The Model:
class srvorders(models.Model):
   client = models.CharField('Client', max_length=200)
   server = models.CharField('Server Type', max_length=200)
   os = models.CharField('Operating System', max_length=200)
   osopts = models.CharField('Operating System Options',
max_length=200, blank=True, null=True)
   ordertotal = models.IntegerField()
   notes = models.TextField('Notes', blank=True, null=True)
   orderdate = models.DateTimeField()
   orderIP = models.IPAddressField()
   status = models.CharField('Order Status', max_length="20")
   srvopts = models.ManyToManyField(srvoptions, blank=True,
null=True)

class Meta:
   ordering = ["id"]
   verbose_name_plural = "Server Orders"

def __unicode__(self):
   return str(self.id)

class Admin:
   pass

The ModelForm:
class NewOrderForm(forms.ModelForm):
   Server_CHOICES = [('', '-- choose a Server --'), ] + [(s.id,
s.name) for s in products.objects.all()]
   OS_CHOICES = [('', '-- choose a Server --'), ] +[(o.id, o.name)
for o in OS.objects.all()]
   OPTION_CHOICES = [('', '-- choose a Server --'), ] +[(opt.id,
opt.name) for opt in osoptions.objects.all()]

server=forms.ChoiceField(choices=Server_CHOICES)
os = forms.ChoiceField(choices=OS_CHOICES)
osopts=forms.ChoiceField(choices=OPTION_CHOICES, required=False)
ordertotal=forms.CharField(required=False)
notes=forms.CharField(required=False)

class Meta:
   model= srvorders
   exclude = ('client', 'orderdate', 'orderIP', 'status')

The View snippet:
if request.method == 'POST':
       form = NewOrderForm(request.POST)
       if form.is_valid():
           new_item = form.save(commit=False)
           new_item.client = orderClient
           new_item.orderdate = datetime.now()
           new_item.orderIP = remote_ip
           new_item.status = "Not_Paid"
           new_item.save()
           form.save_m2m()

Regards,
Leslie

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment