django-fields

How to add a Manager from Field

What i want to do is when some model use my field, it will automaticaly add custom manager to that model. As far as i know, contibute_to_class provide such functionality class MyCustomField(CharField): def contribute_to_class(self, cls, name): super(MyCustomField, self).contribute_to_class(cls, name) setattr(cls, 'c...

How dynamic add custom field to model

How add custom field dynamicly? I'm trying that, but the field won't insert into database when I sync db: #It use as register(MyModel) def register(model, attr="my_attr"): if model in registry: raise AlreadyRegistered( _('The model %s has already been registered.') % model.__name__) registry.append(model) ...

How to markup form fields with <div class='field_type'> in Django

I wasn't able to find a way to identify the type of a field in a django template. My solution was to create a simple filter to access the field and widget class names. I've included the code below in case it's helpful for someone else. Is there a better approach? ## agency/tagutils/templatetags/fieldtags.py ##########################...

Django MultiValueField - How to pass choices to ChoiceField?

I have a multivaluefield with a charfield and choicefield. I need to pass choices to the choicefield constructor, however when I try to pass it into my custom multivaluefield I get an error __init__() got an unexpected keyword argument 'choices'. I know the rest of the code works because when I remove the choices keyword argument from ...

Django MultiWidget Phone Number Field

I want to create a field for phone number input that has 2 text fields (size 3, 3, and 4 respectively) with the common "(" ")" "-" delimiters. Below is my code for the field and the widget, I'm getting the following error when trying to iterate the fields in my form during initial rendering (it happens when the for loop gets to my phone...

Django: How to auto-increment a field that can have multiple of the same value.

I have an app that lets people create tickets, and each Account owner using my app can create tickets. Firstly, should it be unique per account do you think? Or, should it be completely unique? Like this: class Ticket(models.Model): """ An ticket created by an Account holder. """ account = models.OneToOneField('Account...

Django: Adjacents Fields

I made a simple project named "employee" with an "info" app. When I add a new employee the fields comes one below the other, like this: name:____ eno:____ phone1:____ phone2:____ How can I get the output to be like this: name:____ eno:____ phone1:________________ phone2:____________ ...

Django: Would models.Field.validate() for all validation save a lot of code writing?

Am I thinking about this all wrong, or am I missing something really obvious? Python's style guide say's less code is better (and I don't think that's subjective... It's a fact), so consider this. To use forms for all validation means that to write a model field subclass with custom validation, you'd have to: Subclass models.Field Su...

Django: setting required=True on formfield when running clean() .. actually just before

Hi all, I'm trying to force a form field to be required based on a choice widget during validation. def clean(self): cleaned_data = self.cleaned_data if cleaned_data.get('periodical') == True: if cleaned_data.get('period_start_date') == None: msg = _('custom message') self._errors['period_start_d...

Django Piston: How can I exclude nested fields from handler results? Is it even possible?

Before the question, the background: I am putting the finishing touches on an API I have written for a Django app utilizing django-piston. The API is able to search by request or IP address which are Request or IPAddress instances respectively. Each request can have 1 or more IPAddress associated with it. So, for example I have an API...

Django Longint in Mysql

Hi there, my project was not working properly when switching from my local development (sqlite3) to mysql. After some inspection, I found that Django seems to use 32-bit-ints for IntegerField on mysql, but I need 64 bit. Is there any way to achieve this? ...

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

Django1.1 model field value preprocessing before returning

Hi, all. I have a model class like this: class Note(models.Model): author = models.ForeignKey(User, related_name='notes') content = NoteContentField(max_length=256) NoteContentField is a custom sub-class of CharField that override the to_python method in purpose of doing some twitter-text-conversion processing. class NoteCon...

django - dynamic form fieldsets

A form will be spitting out an unknown number of questions to be answered. each question contains a prompt, a value field, and a unit field. The form is built at runtime in the formclass's init method. edit: each questions receives a unique prompt to be used as a label, as well as a unique list of units for the select element. this s...

Django access data passed to form

Hey, I have got a choiceField in my form, where I display filtered data. To filter the data I need two arguments. The first one is not a problem, because I can take it directly from an object, but the second one is dynamically generated. Here is some code: class GroupAdd(forms.Form): def __init__(self, *args, **kwargs): sel...

Django: Model field for storing a list of floats?

I want to store a variable-length list of floats in Django. There is the CommaSeparatedIntegerField, but is there anything like this that I could use? Would it be best to just implement my own CommaSeparetedFloatField or is there something that I am missing completely? Thanks. ...

How to change multiple select field to multiple input fields for a many-to-many case?

When I display the ToolBoxEditForm it uses a multiple select field. But what I want is a form that lets the user edit each tool he has in the toolbox as a text field. I cant figure out how to do this with the many-to-many field. class Tool(models.Model): tool_name = models.CharField(unique=True, max_length=200) ...... class ToolBox...

How to display many-to-many field as a list of text input fields?

When I display the ToolBoxEditForm it uses a multiple select field. But what I want is a form that lets the user edit each tool he has in the toolbox as a text field. I cant figure out how to do this with the many-to-many field. class Tool(models.Model): tool_name = models.CharField(unique=True, max_length=200) ...... class ToolBox...

Can i get models field type from a model queryset in Django?

Can i get model field type from a model queryset in Django? For example: a is b model's queryset and the b model has following fields: f:charfield g:foreignkey h:manytomany Is there any way to get field g's type from queryset a? thx. ...

What is the appropriate numeric field to use

I have a field that sometimes contains integers, sometimes contains decimals and sometimes contains a list of integers. I have been using CommaSeparatedIntegerField and multiplying the decimals by 1000 to get to reasonable integers. Is there a better way? ...