django-models

django views getid

class host(models.Model): emp = models.ForeignKey(getname) def __unicode__(self): return self.topic In views there is the code as, real =[] for emp in my_emp: real.append(host.objects.filter(emp=emp.id)) This above results only the values of emp,My question is that how to get the ids along with emp val...

Django says the "id may not be NULL" but why is it?

I'm going crazy today. I just tried to insert a new record and it threw back a "post_blogpost.id may not be NULL" error. Here's my model: class BlogPost(models.Model): title = models.CharField(max_length=100) slug = models.SlugField(max_length=100) who = models.ForeignKey(User, default=1) when = models.DateTi...

Return template as string - Django

Hi All, I'm still not sure this is the correct way to go about this, maybe not, but I'll ask anyway. I'd like to re-write wordpress (justification: because I can) albeit more simply myself in Django and I'm looking to be able to configure elements in different ways on the page. So for example I might have: Blog models A site update me...

django: CheckboxMultiSelect problem with db queries

firstly sorry for my bad english there is a simple model Person. That contains just languages: LANGUAGE_LIS = ( (1, 'English'), (2, 'Turkish'), (3, 'Spanish') ) class Person(models.Model): languages = models.CharField(max_length=100, choices=LANGUAGE_LIST) #languages is multi value (CheckBoxSelectMultiple) and he...

Sumproduct using Django's aggregation

Question Is it possible using Django's aggregation capabilities to calculate a sumproduct? Background I am modeling an invoice, which can contain multiple items. The many-to-many relationship between the Invoice and Item models is handled through the InvoiceItem intermediary table. The total amount of the invoice—amount_invoiced—i...

Subclassed django models with integrated querysets

Like in this question, except I want to be able to have querysets that return a mixed body of objects: >>> Product.objects.all() [<SimpleProduct: ...>, <OtherProduct: ...>, <BlueProduct: ...>, ...] I figured out that I can't just set Product.Meta.abstract to true or otherwise just OR together querysets of differing objects. Fine, but...

ManyToManyField "table exist" error on syncdb

When I include a ModelToModelField to one of my models the following error is thrown. Traceback (most recent call last): File "manage.py", line 11, in <module> execute_manager(settings) File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 362, in execute_manager utility.execute() File "/Library/Pytho...

IntegrityError: foreign key violation upon delete

I have Order and Shipment model. Shipment has a foreign key to Order. class Order(...): ... class Shipment() order = m.ForeignKey('Order') ... Now in one of my views I want do delete order object along with all related objects. So I invoke order.delete(). I have Django 1.0.4, PostgreSQL 8.4 and I use transaction middleware...

Generate a few models from existing database in Django

I know this exists django-admin.py inspectdb > models.py However, is there an easy way to limit it? Without manually deleting what I don't want. I'm connecting to a database that has over one hundred tables, but I only want models of about 4 or 5. Is there an easy way to generate models from a few given tables? They are quite big ta...

Django - How best to handle ValidationErrors after form.save(commit=False)

This is a fragment of my code from a view: if form.is_valid(): instance = form.save(commit=False) try: instance.account = request.account instance.full_clean() except ValidationError, e: # Do something with the errors here... I certainly don't want to do this 180 times. T...

figuring out objects created 30 min ago in django

I have a DateTimeField called created in my model and I would like to get all the objects where created date is 30 min or more. How would query this using MyModel.objects(....) in django? ...

django model fields comparison

is there a way l can compare two columns | fields in django like Invoice.objects.filter(amountdue_lt=invoiceamount) where amountdue and invoiceamount are two columns of Invoice object Thanks ...

Django JSON serializable error

With the following code below, There is an error saying File "/home/user/web_pro/info/views.py", line 184, in headerview, raise TypeError("%r is not JSON serializable" % (o,)) TypeError: <lastname: jerry> is not JSON serializable In the models code header(models.Model): firstname = models.ForeignKey(Firstname)...

ForeignKey django 1.1.1 model

I have class staff_name(models.Model): firstname = models.CharField(max_length=150) surname = models.CharField(max_length=150) class inventory_transaction(models.Model): staffs = models.ForeignKey(staff_name) I want to get or create staff surname and first name through inventory_transaction I used these code below ...

Django automatically compress Model Field on save() and decompress when field is accessed

Given a Django model likeso: from django.db import models class MyModel(models.Model): textfield = models.TextField() How can one automatically compress textfield (e.g. with zlib) on save() and decompress it when the property textfield is accessed (i.e. not on load), with a workflow like this: m = MyModel() textfield = "Hello, ...

Django queryset to find a char value with filtered out spaces?

There are database string values that are sometimes stored with unnecessary spaces: "SDF@#$#@ 132423" Given the value without spaces in the UI of a program: "SDF@#$#@132423" How could I do a Django queryset filter to find the Database value (with spaces) from the UI input value sans spaces? ...

ProgrammingError when aggregating over an annotated & grouped Django ORM query

I'm trying to construct a query to get the "average, maximum and minimum number of items purchased per user". The data source is this simple sales records table: class SalesRecord(models.Model): id = models.IntegerField(primary_key=True) user_id = models.IntegerField() product_code = models.CharField() pr...

Querying many to many fields in django

In the models there is a many to many fields as, from emp.models import Name def info(request): name = models.ManyToManyField(Name) And in emp.models the schema is as class Name(models.Model): name = models.CharField(max_length=512) def __unicode__(self): return self.name Now when i want to query a...

cannot append Model.objects.all()

I cannot run ['abc'].append( MyModel.objects.all() ) since it generates exception 'NoneType' object is not iterable if MyModel has no entry. any workaround or something like ? : in c++ edit: my statement is actually ','.join([ str(e) for e in ['abc','def'].append( MyModel.objects.all() ) ]) it seems that the problem is caused by ...

Django - Threading in views without hanging the server

One of my applications in my Django project require each request/visitor to that instance to have their own thread. This might sound confusing, so I'll describe what I'm looking to accomplish in a case based scenario, with steps: User visits application Thread starts Until the thread finishes, that user's server instance hangs Once the...