possible model relations?

2010-10-06 Thread alecx
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.



MultiFileInput widget

2010-10-16 Thread alecx
Hi, I have only a few weeks django/web experience and currently
struggling with a specific problem. I dont know how to solve it. and
therefore I ask you experts.
I have an Itemform (ModelForm) with a fileupload field. But I want to
upload multiple files with multifileinput (djangosnippet:583). Can I
use this widget with my standard model or do I have to create a
Fileupload model with a foreignkey relation to the Itemform?

-- 
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.



Re: MultiFileInput widget

2010-10-19 Thread alecx
Okay, I am answering myself. Maybe this info helps someone else.
I created a fileupload model with foreignkey relation.
# models.py
class Item(models.Model):
serialnr = models.CharField(max_length=5, blank=True)


class Fileupload(models.Model):
item = models.ForeignKey(Item)
file = models.FileField(upload_to='uploads/%Y/%m/%d', blank=True)
...
# views.py
def item_create(request):
if request.method == 'POST':
form = ItemForm(request.POST)
fileform = FileuploadForm(request.FILES)
...
form.save()
id = new_item.id
for f in request.FILES.getlist('file[]'):
 fileform = Fileupload(file=f)
 fileform.item_id = id
 fileform.save()
# forms.py
class FileuploadForm(ModelForm):
file = forms.FileField(widget=MultiFileInput, required=False)
class Meta:
model = Fileupload
new_item.save()
return HttpResponseRedirect('/tracker/')
...

-- 
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.



distinct in autocomplete

2010-10-20 Thread alecx
Hello,

how do I get only distinct values of that query in the autocomplete
field:

def autocomplete_techlastname(request):
if request.GET.has_key('q'):
tags = 
Item.objects.filter(techlastname__icontains=request.GET['q'])
[:5]
return HttpResponse('\n'.join(tag.techlastname for tag in tags))
return HttpResponse()

appending distinct seems not to work.

-- 
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.



how do i get access to json data?

2010-11-23 Thread alecx
Hello django users,

after several try outs, i am not able to figure out how to access data
in a serialized object.
E. g. how do I get the name "Douglas", "Adams" in the example:
http://docs.djangoproject.com/en/dev/topics/serialization/#deserialization-of-natural-keys

In javascript i can get e. g. the "name": "Mostly Harmless" with:
this.fields.name.
But how do i get the "author": ["Douglas", "Adams"]?
I tried serveral options, but had no luck.


-- 
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.



Re: how do i get access to json data?

2010-11-23 Thread alecx
Thanks for your answer Li.
This solution works for python, I figured this out too.

But how does this work inside a template with javascript?
I use $.getJSON() inside the template to get the data.
Inside the javascript I can access the data with:
   this.fields.name
but
   this.fields.author
does not work.

Any idea?




On 23 Nov., 15:05, Li You  wrote:
> sorry but a mistake.
>
>
>
> On Tue, Nov 23, 2010 at 10:01 PM, Li You  wrote:
> > Maybe this is what you want:
>
> > from json import JSONDecoder
> > s = '''{
> >    "pk": 1,
> >    "model": "store.book",
> >    "fields": {
> >        "name": "Mostly Harmless",
> >        "author": ["Douglas", "Adams"]
> >    }
> > }
> > '''
> > d = JSONDecoder().decode(s)
>
> > print a['fields']['author']
>
> ~~~ should be
> print d['fields']['author']
>
>
>
>
>
> > On Tue, Nov 23, 2010 at 9:45 PM, alecx
> >  wrote:
> >> Hello django users,
>
> >> after several try outs, i am not able to figure out how to access data
> >> in a serialized object.
> >> E. g. how do I get the name "Douglas", "Adams" in the example:
> >>http://docs.djangoproject.com/en/dev/topics/serialization/#deserializ...
>
> >> In javascript i can get e. g. the "name": "Mostly Harmless" with:
> >> this.fields.name.
> >> But how do i get the "author": ["Douglas", "Adams"]?
> >> I tried serveral options, but had no luck.
>
> >> --
> >> 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 
> >> athttp://groups.google.com/group/django-users?hl=en.
>
> > --
> > Best Regards!
>
> > Li You
> > University of Science and Technology of China
>
> --
> Best Regards!
>
> Li You
> University of Science and Technology of China

-- 
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.



tinymce

2010-12-17 Thread alecx
Hi,

I integrated django-tinymce into my application. Now I can edit the
text in the former textarea field.
But when I submit the form, the field is empty.
I am not able to figure out why the text I wrote in the editor field
is not submitted.
Has someone a tip for me?
Thanks in advance.

-- 
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.