Hello,
I try to build a kind of call tracker app. The call intaker creates a
new item (main model) and makes some notes (subject, issue and which
printer nr, customer, technician,... all with modelfields) all in one
view. But I am struggling with the model relations and the form
saving. I cannot save anything to the db, thats because one related
object isnt already saved to the db.
I think the problem is the m2m relation between Servicetechnician and
Customer. Can someone please point me to the right direction?

Thank you.

# models
class Servicetechnician(models.Model):
        printers = models.ManyToManyField('Printer')
        firstname = models.CharField(max_length=30, blank=True)
        lastname = models.CharField(max_length=30, blank=True)
        email = models.EmailField(blank=True)
        telephonenr = models.CharField(max_length=30, blank=True)
...
class Customer(models.Model):
        technicians = models.ManyToManyField(Servicetechnician)
        name = models.CharField(max_length=30, blank=True)
        town = models.CharField(max_length=30, blank=True)
        country = models.CharField(max_length=20, blank=True)
...
class Printer(models.Model):
        serialnr = models.CharField(max_length=5, blank=True)
        productname = models.CharField(max_length=15, blank=True)
        customer = models.ForeignKey(Customer)
...
class Fileupload(models.Model):
        item = models.ForeignKey('Item')
        file = models.FileField(upload_to='uploads/%Y/%m/%d', blank=True)
...
class Item(models.Model):
        servicetechnician = models.OneToOneField(Servicetechnician)
        customer = models.OneToOneField(Customer)
        printer = models.OneToOneField(Printer)
        subject = models.CharField(max_length=30, blank=False)
        issue = models.TextField(blank=False)
        author = models.ForeignKey(User, related_name='item_poster')
...

# view
@staff_member_required
def item_create(request):
        if request.method == 'POST':
                form = ItemForm(request.POST, request.FILES, prefix='item')
                printerform = PrinterForm(request.POST, prefix='printer')
                technicianform = ServicetechnicianForm(request.POST,
prefix='technician')
                customerform = CustomerForm(request.POST, prefix='customer')
                fileform = FileuploadForm(request.POST, request.FILES,
prefix='files')
                #import pdb; pdb.set_trace()
                if form.is_valid() and printerform.is_valid() and
technicianform.is_valid() and customerform.is_valid() and
fileform.is_valid():
                        print "all validation passed!"

                        new_item = form.save(commit=False)
                        new_customer = customerform.save(commit=False)
                        new_printer = printerform.save(commit=False)
                        new_technician = technicianform.save(commit=False)
                        new_files = fileform.save(commit=False)

                        new_item.author = request.user
                        new_item.save()

                        new_technician.printers = new_printer
                        new_technician.save()

                        new_customer.technicians = new_technician
                        new_customer.save()

                        new_printer.customer = new_customer
                        new_printer.save()

                        for f in request.FILES.getlist('file[]'):
                                fileupload = Fileupload.objects.create()
                                upload_file = f
                                fileupload.file.save(upload_file.name, 
upload_file)

                        new_files.item = new_item
                        new_files.save
                        form.save_m2m()
                return HttpResponseRedirect('/tracker/')
        else:
                form = ItemForm(prefix='item')
                printerform = PrinterForm(prefix='printer')
                technicianform = ServicetechnicianForm(prefix='technician')
                customerform = CustomerForm(prefix='customer')
                fileform = FileuploadForm(prefix='files')
        return render_to_response('tracker/item_create.html', {'form':form,
'fileform':fileform, 'technicianform': technicianform,
'customerform':customerform, 'printerform':printerform},
context_instance=RequestContext(request))

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

Reply via email to