django-models

Django - dumpdata truncate to last n lines.

Hi all, Does anyone have a simple solution to using (or modifying) dumpdata to trucate a simple table down to the last n lines. I like to use dump data for test fixtures but the data size has gotten so large this doesn't make sense. BTW - I didn't design the table I'm just the sucker which has to deal with it. For those who may ask h...

Combining User and UserProfile in the admin

I have been reading up on Django's separation of Users and Profiles, and I have decided to go with a model called UserProfile that is located in an Accounts app as my Profile. The problem is, now I have two separate areas of the admin, one for modifying the User, and one for modifying the User Profile. Is it possible to view the two mo...

django most efficient way to count same field values in a query

Lets say if I have a model that has lots of fields, but I only care about a charfield. Lets say that charfield can be anything so I don't know the possible values, but I know that the values frequently overlap. So I could have 20 objects with "abc" and 10 objects with "xyz" or I could have 50 objects with "def" and 80 with "stu" and i ha...

How to bulk create model objects in django

Hi all, I have a lot of objects to save in database, and so I want to create Model instances with that. With django, I can create all the models instances, with MyModel(data), and then I want to save them all. Currently, I have something like that: for item in items: object = MyModel(name=item.name) object.save() I'm wonde...

unique_together foreign key object properties

I've got two models: Common and ARecord. ARecord has a ForeignKey relationship to Common. I want to ensure that ARecord is unique with a combination of items from ARecord and Common. class Common(models.Model): NAIC_number = models.CharField(max_length=5) file_location_state = models.CharField(max_length=2) file_location_...

Django QuerySet .defer() problem - bug or feature?

An example is better than a thousand words: In [3]: User.objects.filter(id=19)[0] == User.objects.filter(id=19)[0] Out[3]: True In [4]: User.objects.filter(id=19)[0] == User.objects.filter(id=19).defer('email')[0] Out[4]: False Does it work like this on purpose ? Subquestion: is there any simple way to get a regular mode...

Django: Exposing model method to admin.

Example model: class Contestant(models.Model): first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) email = models.EmailField() ... def send_registration_email(self): ... I'd like to be able to expose this method to the admin so that managers can login and manually cal...

Django model field with default value set violates not null constraint when saved.

My field: signup_date = models.DateTimeField(blank=True,default=datetime.now) My error when saving: IntegrityError: null value in column "signup_date" violates not-null constraint I'm trying to make a simple unit test where I create a bound instance of a ModelForm from a dict and save it. Thanks. Traceback (most recent call last)...

django-photologue upload_to

Hi all. I have been playing around with django-photologue for a while, and find this a great alternative to all other image handlings apps out there. One thing though, I also use django-cumulus to push my uploads to my CDN instead of running it on my local machine / server. When I used imagekit, I could always pass a upload_to='whatev...

Django FloatField in ModelForm with 2 precision point

Hello All, I have a FloatField in one of my model. I Add/Edit it using ModelForms. What I need is that, when i edit, the Float value stored in FloatField to be rendered with exactly 2 precision points. i.e. if my stored value is 1000.0 or 1000.123, i want the input field of form to show initial value of 1000.00 or 1000.12. Any suggest...

Having a field named 'created' within a model - Django

Hi folks, I have a model such as the following: class Item(models.Model): name = models.CharField(max_length=150) created = models.DateTimeField(auto_now_add=True) the admin class is the following: class ItemAdmin(admin.ModelAdmin): list_display = ('name', 'created') the created field does not seem to exist Is there...

Data arrays in Django model

I am just starting out with Django and would like to know the best way to deal with the following data. I have DataSets which are comprised of many x,y coordinate pairs which I wish to plot. It is my understanding that Django doesn't support numeric arrays directly in it's models so what is the best way to deal with these? Right now all ...

Multiple Database Config in Django 1.2

This is hopefully an easy question. I'm having some trouble understanding the documentation for the new multiple database feature in Django 1.2. Primarily, I cant seem to find an example of how you actually USE the second database in one of your models. When I define a new class in my models.py how do I specify which database I intend ...

Query filtering in Django with sqlite

I've tried the following query with Django, def search(search_text): q = Info.objects.filter(title__contains=str(search_text)) print q.query The query that get printed is SELECT "d"."id", "d"."foo" FROM "d_info" WHERE "d_info"."title" LIKE %hello% ESCAPE '\' The query fails because the text after LIKE doesn't have quotes ...

Django: Count objects in a particular month

I have this model: class Person(models.Model): city = models.CharField(max_length=20, blank=True) added_date = models.DateField(default=datetime.date.today) I want to create a template/view that has a table of months and the number of people added that month (ie, 5 in january, 10 in february, 8 in march, etc.). I have a similar ...

Return a model from a custom query without hitting the database

Hi, I have a custom query which eventually returns a list of objects. I need the function to return the actual objects but I don't want to hit the database twice for every query since it's already an expensive query. How can i return a model instance without hitting the db? NB: I presume doing something like the following will actually...

Simple Django form / model save question

I want to set the BooleanField inuse to True when I save the ModelForm (I'm using a form outside of the admin area) and I'm unsure how to do it. Models: class Location(models.Model): place = models.CharField(max_length=100) inuse = models.BooleanField() class Booking(models.Model): name = models.CharField(max_length=100, v...

Django update queryset with annotation

I want to update all rows in queryset by using annotated value. I have a simple models: class Relation(models.Model): rating = models.IntegerField(default=0) class SignRelation(models.Model): relation = models.ForeignKey(Relation, related_name='sign_relations') rating = models.IntegerField(default=0) And I want to awoid ...

How to provide model dependency information for use by Django's dumpdata?

Attempting to run the dumpdata command in Django release 1.2.1: ./manage.py dumpdata myapp I get the error "Can't resolve dependencies for myapp.model1 myapp.model2 myapp.model3". Dumpdata is fine if I specify the list of models - e.g., ./manage dumpdata myapp.model1 myapp.model2 myapp.model3 etc. Is there a way to encode the de...

Cleaning data which is of type URLField

I have a simple URLField in my model link = models.URLField(verify_exists = False, max_length = 225) I would like to strip the leading and trailing spaces from the field. I don't think I can do this in "clean_fieldname" or in the "clean" method. Do I need to sub-class the "URLField" and remove the spaces in to_python method? Is ther...