django-models

What are good ways to upload bulk .csv data into a webapp using Django/Python?

I have a very basic CSV file upload module working to bulk upload my user's data into my site. I process the CSV file in the backend with a python script that runs on crontab and then email the user the results of the bulk upload. This process works ok operationally, but my issue is with the format of the csv file. Are there good to...

Django model default sort order using related table field

Is it possible to set the default sort order for a model to a field from a related model (rather than the integer key) i.e. something that yields a SQL order by clause with a field from both models? If so, how? I can do this via query_by but I can't figure out how to set it by default. Thanks. class Foo(models.Model): name = models....

Django - designing models with virtual fields?

Hi fellow stackers, I'd like to ask about the most elegant approach when it comes to designing models with virtual fields such as below in Django... Let's say we're building an online store and all the products in the system are defined by the model "Product". class Product(models.Model): # common fields that all products share ...

In a django site I want to let users create other users that are tied to their accounts.

I want to let a logged-in and registered user create extra user accounts that he will be the admin of. These accounts will be special "subordinate" accounts that are tied to the user creating them. He should be able to add/modify/delete these accounts kind of like the theory of how a Google apps administrator manages the accounts for h...

Fixing the auth_permission table after renaming a model in Django

Every now and then, you have the need to rename a model in Django (or, in one recent case I encountered, split one model into two, with new/different names). Yes, proper planning helps to avoid this situation, but sometimes reality intervenes. After renaming corresponding tables in the db and fixing affected code, one problem remains: ...

Django: Querying read-only view with no primary key

class dbview(models.Model): # field definitions omitted for brevity class Meta: db_table = 'read_only_view' def main(request): result = dbview.objects.all() Caught an exception while rendering: (1054, "Unknown column 'read_only_view.id' in 'field list'") There is no primary key I can see in the view. Is there a wo...

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 to get './manage.py syncdb' to create additional views or run custom SQL?

Is there a way to run some custom SQL statements after syncdb does it thing creating the tables for the models? Specifically, I would like to create some database views. Thanks in advanced. ...

Django Model Inheritance. Hiding or removing fields.

I want to inherit a model class from some 3rd party code. I won't be using some of the fields but want my client to be able to edit the model in Admin. Is the best bet to hide them from Admin or can I actually prevent them being created in the first place? Additionally - what can I do if one of the unwanted fields is required? My first ...

Manager Returns Model Objects Once Per Request (Manager Drops Objects After Returning Them Once)

The behavior was unrelated to the problem as presented immediately below. See the bottom of the post for an explanation. thanks. Hello, I am currently experiencing the behavior that the default Manager for a particular Model returns the objects for this Model only once per request or per shell session. Below is a PDB transcript fr...

Django labels and translations - Model Design

Lets say I have the following Django model: class StandardLabel(models.Model): id = models.AutoField(primary_key=True) label = models.CharField(max_length=255) abbreviation = models.CharField(max_length=255) Each label has an ID number, the label text, and an abbreviation. Now, I want to have these labels translatable into...

What test scenarios are necessary and sufficient to exhaustively black box test a recurring appointment model?

I have a django model for an appointment in a calendar that I am attempting to write a very comprehensive test driver for. The recurring appointment occurs at some point in time and can either run on infinitely or recur for a fixed number of times. The appointment mirrors the functionality available for a Google Calendar appointment (...

What is the best way to represent a schedule in a database, via Python/Django?

I am writing a backup system in Python, with a Django front-end. I have decided to implement the scheduling in a slightly strange way - the client will poll the server (every 10 minutes or so), for a list of backups that need doing. The server will only respond when the time to backup is reached. This is to keep the system platform indep...

Increment Page Hit Count in Django

I have a table with an IntegerField (hit_count), and when a page is visited (ie. http://site/page/3) I want record id 3 'hit_count' column in the database to increment by 1. The query should be like: update table set hit_count = hit_count + 1 where id=3 Can I do this with the standard Django Model conventions? Or should I just write th...

BI with Django?

Is there a way to develop Bi (Business Intelligence) solutions with Django? Therefore it should be possible to define models with more than one Datasource. Is anybody out there who has experienced BI with Django? How could it work ? ...

Django Form Preview - How to work with 'cleaned_data'

Thanks to Insin for answering a previous question related to this one. His answer worked and works well, however, I'm perplexed at the provision of 'cleaned_data', or more precisely, how to use it? class RegistrationFormPreview(FormPreview): preview_template = 'workshops/workshop_register_preview.html' form_template = ...

Returning a nicely punctuated string from an object query.

In my django app, especially on the admin side, I do a few def's with my models: def get_flora(self): return self.flora.all() def targeted_flora(self): return u"%s" % (self.get_flora()) whereas flora is a ManyToManyField, however, sometimes ForeignKey fields are used also. I do this to provide a utility 'get' function for th...

Django Models - Conditionally adding an object to a ManyToManyField before saving

Hello The following does not quite work, its purpose is: Upon save, check for the existence of 'supervisor' in the 'operators', and add it too them if not. class JobRecord(models.Model): """JobRecord model""" project = models.ForeignKey(Project) date = models.DateField() supervisor ...

Django Models - Problem using ManyToManyField add() method

Hi, I have a problem using the add() method below... class JobRecord(models.Model): """JobRecord model""" project = models.ForeignKey(Project) date = models.DateField() supervisor = models.ForeignKey(User, related_name='supervisor_set') operators = models.ManyToManyFiel...

Django Models - FK default to the last entered record (by the logged in user)

Hi there Stackers! I have 2 models in question. JobRecord of which has many Activity(ies), the fk, of course, placed in the Activity model. To aid data entry, I'd like to default the fk to JobRecord, within a new Activity, to the 'JobRecord fk of the last entered Activity, by the logged in User (bonus points!)' Here's the model: cla...