django

How to access request in ModelForm for adding request.user as foreign key

I am trying to override save in the modelform to add the current user as the owner of a vehicle. But I am receiving 'NoneType' object has no attribute 'user' What am I forgetting? forms.py: class VehicleForm(ModelForm): class Meta: model = Vehicle exclude = ('slug', 'owner', ) def __init__(self, *args, **kwargs...

django forms exclude values from choicefield

I have the following model class ActionConfirm(models.Model): CONFIRM_METHOD = ( (u'ce', u'Certificate'), (u'tf', u'Trainee Feedback'), (u'ms', u'Multi Source Feedback'), (u'rp', u'Reflection upon Practice'), (u'ot', u'Other - Please add/describe') ) confirm_method = models.CharField...

Django, how to group by day

Hello Guys, I've tried the following with no success: Match.objects.filter(sendDate__gte=dateToStats).values("sendDate__day").annotate(perDay=Count("id")).order_by() Fails with: Cannot resolve keyword 'sendDate__day' into field. Where sendDate is a DateTime field, and dateToStats is just a certain date I'm filtering. I'm intereste...

model save method. Resize image before save

Hi. I am trying to resize an image before it gets saved. I am using a custom save method on the model, but I've hit a snag. This is the code I have now: class UserImages(models.Model): height = models.CharField(blank=True, max_length=100) width = models.CharField(blank=True, max_length=100) image = models.ImageField(upload_...

Running manage.py dumpdata on apps with dots in their names

I'm trying to do moving some data from my development machine to a server using dumpdata but ran into a problem. So, say I want to dump the data that belongs to the app django.contrib.auth. django.contrib.auth is in my INSTALLED_APPS. This happens when I run $ python manage.py dumpdata django.contrib.auth Error: Unknown application: dja...

Django model filter, depending on exsisteing relation.

How can I ONLY the categories where there is at least one "Post" related??, hope it makes sense!? **models.py** class Category(models.Model): name = models.CharField(max_length=50) def __unicode__(self): return self.name class Post(models.Model): name = models.CharField(max_length=50) categories = models.ManyToManyFie...

Django design question

Hi, I have a model in django that have a boolean private/public attribute: class TestModel(models.Model): name = models.CharField() is_public = models.BooleanField(default=False) I want that every-time I query this model in an application it returns only public instances to the common user and all available instances to the su...

Django templates split text on fullstop

Using django templates I would like to split a block of text on the first fullstop. I would then like to give the first sentence a class of highlight and then the rest of the text a class of normal. how would i do this? e.g. (I know this doesn't work) text = Aliquam pretium vestibulum nibh, vel molestie velit varius nec Curabitur non n...

How make QuerySet of items which contain other model.

So i have one model with list of all item i have. And other model which has Foreign-Key to to my main model... How to Make query set object. Of all items from first model which contain my second model? class Music(models.Model): name=models.CharField() duration=models.IntegerField() class Playlist(models.Model): misic=mode...

Django Testing: determine which view was executed

In the Django testing documentation they promise that you can "Test that the correct view is executed for a given URL." However I didn't find any possibility how to test which view was executed. I would expect that in the Response class but there's nothing about the executed view. Thanks in advance. ...

passing a callback as upload_to to FileField

I have an abstract model class UploadItem for handling uploaded files. I want each subclass to be able to define the upload_to path. For this, i pass a callback to the constructor of FileField. This is an example: class UploadItem(models.Model): file = models.FileField(upload_to=UploadItem.get_directory) class Meta: ...

Django , with jQuery Basic help !!

Hello , I am working on quick project , listing search results from bing and my goal is user can save searches they perform . however i manage to list the search results in the template , and right now want to save the result items to the database using AJAX ! well this is my first attempt with ajax ( jquery ) and django ! template {...

what files in django are not necessary for using only templates?

hi, I use django templates 1.2 in my GAE app. I remember default templates in GAE were some kind of old version (0.96 or smth) and I moved to the new one. The question is - what files and directories I can delete from django folder to minimize its size - I need only templates. ...

Why Django's ModelAdmin uses lists over tuples and vice-versa

From the Django intro tutorial, in \mysite\polls\admin.py: from django.contrib import admin #... class PollAdmin(admin.ModelAdmin): #... inlines = [ChoiceInline] list_display = ('question', 'pub_date', 'was_published_today') list_filter = ['pub_date'] admin.site.register(Poll, PollAdmin) Why do inlines and list_filter both us...

django magic: context passed to template

Hi, There is something i don't get in django template system. I have a FileField in my model called myfile. If i pass an instance of my model to a template, i can access file.size (this is an example). Form where this variable 'size' come from?? it's not part of the FileField class as far as i know. A small test: def save(self): s...

Django / Python, Using Radio Button for boolean field in Modelform?

I'm trying to use a radio button in my modelform but its just outputting nothing when I do an override this way (it just prints the label in my form, not the radio buttosn, if I don't do the override it does a standard checkbox) My modelfield is defined as: Class Mymodelname (models.Model): fieldname = models.BooleanField(max_lengt...

Rails or Django style routing in Perl

I have grown accustomed to the way Rails maps a route or the Django uses regex on a route (I am not expect in Django, but this is what I have heard how it does it's routing) and how they use the style of permalinks to access a particle web page. Is it possible to do the same thing in Perl? ...

List of months in Django

I'm trying to have a select form with a list of months but I can't seem to get it to post correctly. form: class MonthForm(forms.Form): months = [('January','January'), ('February','February'), ('March','March'), ('April','April'), ('May','May'), ...

How does django convert string to modules

Hi, I am trying to understand an other magic thing about django: it can convert strings to modules. In settings.py, INSTALLED_APPS is declared like that: INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', ) All it contains is strings. But django will convert those strings to...

How to manipulate the response object in django-piston?

I have some existing python code that uses django-piston which returns a dictionary as its response. For example: from piston.handler import BaseHandler class FooHandler(BaseHandler): allowed_methods = ('GET',) @classmethod def create(self, request): return { 'foo': 'bar' } This code works fine, and is serialized...