django-models

Auto-populating created_by field with Django admin site

I want to use the Django admin interface for a very simple web application but I can't get around a problem that should not be that hard to resolve .. Consider the following: class Contact(models.Model): name = models.CharField(max_length=250, blank=False) created_by = models.ForeignKey(User, blank=False) I can't find a way t...

How to save django FileField to user folder?

I've got a model like this def upload_location(instance, filename): return 'validate/%s/builds/%s' % (get_current_user(), filename) class MidletPair(models.Model): jad_file = models.FileField(upload_to = upload_location) jar_file = models.FileField(upload_to = upload_location) upload_to=tempfile.gettempdir() How...

About 20 models in 1 django app

I have started work on a local app for myself that runs through the browser. Having recently gone through the django tutorial I'm thinking that it might be better to use django rather than just plain python. There's one problem, I have at least 20 models and each will have many functions. Quite simply it's going to create one huge model...

Can a Django model automatically fill in the current authenticated user from the admin interface?

I'd like to be able to include a reference to the currently authenticated user with a Note when working with Notes from the admin interface. The model would look something like: from django.db import models from django.contrib.auth.models import User from datetime import datetime class Note(models.Model): datetime = models.DateTi...

Django: Populate user ID when saving a model

Hello, I have a model with a created_by field that is linked to the standard Django User model. I need to automatically populate this with the ID of the current User when the model is saved. I can't do this at the Admin layer, as most parts of the site will not use the built-in Admin. Can anyone advise on how I should go about this? ...

url method of ImageField returns a non-Url value - Django

I'm developing using Django on Windows. I have a model with an imagefield, and use a form to fill it. Images get uploaded without problem. The problem occurs when I attempt to show an uploaded image inside a template by coding this: <img src ='{{object.image.url}}'/> (object is an instance of the relevant model, and image is the name ...

how to modify choices on admin pages - django

I have a model that has a field named "state": class Foo(models.Model): ... state = models.IntegerField(choices = STATES) ... For every state, possible choices are a certain subset of all STATES. For example: if foo.state == STATES.OPEN: #if foo is open, possible states are CLOSED, CANCELED ... if foo.state == STA...

keyerror inside django model class __init__

Here's a Django model class I wrote. This class gets a keyerror when I call get_object_or_404 from Django (I conceive that keyerror is raised due to no kwargs being passed to __init__ by the get function, arguments are all positional). Interestingly, it does not get an error when I call get_object_or_404 from console. I wonder why, and ...

In Django, how can you change the User class to work with a different db table?

We're running django alongside - and sharing a database with - an existing application. And we want to use an existing "user" table (not Django's own) to store user information. It looks like it's possible to change the name of the table that Django uses, in the Meta class of the User definition. But we'd prefer not to change the Dja...

How to prevent self (recursive) selection for FK / MTM fields in the Django Admin

Given a model with ForeignKeyField (FKF) or ManyToManyField (MTMF) fields with a foreignkey to 'self' how can I prevent self (recursive) selection within the Django Admin (admin). In short, it should be possible to prevent self (recursive) selection of a model instance in the admin. This applies when editing existing instances of a mod...

Zlib in database - Django

When I try to put a zlibbed string in models.TextField >>> f = VCFile(head = 'blahblah'.encode('zlib')) >>> f.save() it fails: ... raise DjangoUnicodeDecodeError(s, *e.args) DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0x9c in position 1: unexpected code byte. You passed in 'x\x9cK\xcaI\xccH\x02b\x00\x0eP\x03/' (<type...

how to use filter in django

hi, class Status(models.Model): someid = models.IntegerField() value = models.IntegerField() status_msg = models.CharField(max_length = 2000) so my database look like: 20 1234567890 'some mdg' 20 4597434534 'some msg2' 20 3453945934 'sdfgsdf' 10 4503485344 'ddfgg' so I have to fetch values contain a givin...

Custom ordering in Django

How do you define a specific ordering in Django QuerySets? Specifically, if I have a QuerySet like so: ['a10', 'a1', 'a2']. Regular order (using Whatever.objects.order_by('someField')) will give me ['a1', 'a10', 'a2'], while I am looking for: ['a1', 'a2', 'a10']. What is the proper way to define my own ordering technique? ...

python orm

This is a newbie theory question - I'm just starting to use Python and looking into Django and orm. Question: If I develop my objects and through additional development modify the base object structures, inheritance, etc. - would Django's ORM solution modify the database automatically OR do I need to perform a conversion (if the app is ...

Django - counting model instance views (for a "top entries" app)

I'm new, and confused. I want to create a module that keeps track of the "top hit" instances of both an article and a blog model. I don't want to touch the code for the article or blog models. Is this a candidate for middleware? looking at the HttpRequest.path? Thanks ...

LEFT OUTER JOIN with additional definitions in Django

Hi! I have this models: class Project(models.Model): users = models.ManyToManyField(User, through='Project_User') class Project_User(models.Model): project = models.ForeignKey('Project') user = models.ForeignKey(User) property = models.BooleanField() Not all Projects have own Project_User rows. Thing, what I need is ...

How to change default django User model to fit my needs?

The default Django's User model has some fields, and validation rules, that I don't really need. I want to make registration as simple as possible, i.e. require either email or username, or phone number - all those being unique, hence good as user identifiers. I also don't like default character set for user name that is validated in D...

Unable to call custom method

Hi, I am newbie at Django.I have model with a custom method.In view I am retrieving a single object.See below -- My model class Problem(models.Model): problem = models.CharField(max_length=100) solution=models.CharField(max_length=500) def unicode(self): return self.problem def retrieve_rankdata(self): ...

In a django model custom save() method, how should you identify a new object?

I want to trigger a special action in the save() method of a Django model object when I'm saving a new record (not updating an existing record.) Is the check for (self.id != None) necessary and sufficient to guarantee the self record is new and not being updated? Any special cases this might overlook? ...

django inline issue

I have made a inline named as Fooinline. This inline was working fine in Django 1.02 but as soon as I upgraded to Django 1.1 it started giving an error: **TypeError at /admin/casd/aaas/4028cb901dd9720a011deadd85e8007f/ __init__() got an unexpected keyword argument 'request'** My Fooinline code is: class FooInline(InlineModelAdmin): ...