django-models

Paging depending on grouping of items in Django

For a website implemented in Django/Python we have the following requirement: On a view page there are 15 messages per web paging shown. When there are more two or more messages from the same source, that follow each other on the view, they should be grouped together. Maybe not clear, but with the following exemple it might be: An ex...

Django, Python Loop Logic Problem

This works, partially. More information may be needed, however, I thought I would post to get advice on anything obvious that might be wrong here. The problem is that if activity.get_cost() returns a False value, the function seems to exit entirely, returning None. What I'd like it to do, of course, is accumulate cost Decimal values i...

help with complex join in Django ORM

class Domains(models.Model): name = models.CharField(max_length=30) description = models.CharField(max_length= 60) user = models.ManyToManyField("Users", blank=True, null=True) def __unicode__(self): return self.name class Groups(models.Model): domain = models.ForeignKey(Domains) name = models.CharField(m...

Using ModelForm and passing arguments

class MyUserAdminForm(forms.ModelForm): class Meta: model = Users group = forms.ModelMultipleChoiceField( queryset=Groups.objects.filter(domain__user=3), widget=forms.CheckboxSelectMultiple, ) class UserAdmin(admin.ModelAdmin): list_display = ('login', 'company', 'userType') form = MyUserAdminForm filter_horizontal = ('group',) a...

dynamic model creation in django

Hi any body know how to create a dynamic model in django or any documentation for this ...

Django Admin: Managing database

I am using Django admin for managing my data. I have the following tables: Users, Groups, and Domains. Users has a many-to-many relationship with both Groups and Domains. Domains has a one-to-many relationship with Groups. When I remove a User from a Domain, I also want to remove any entries in Users_Groups for that particular User and G...

Help with deleting reconds in Django

for u in Users.objects.all(): for g in u.group.all(): if g not in Groups.objects.filter(domain__user=u.id): u.group.filter(id=g.id).delete() How do I delete the entries in relationship table. In this case I have a many to many relationship between Groups and Users. The delete statement in the above code delete...

Django Admin Form for Many to many relationship

I have a many to many relationship between 2 tables Users an Domains. I have defined this relationship in the Domains class. So in the admin interface I see the Users when I am viewing Domains. But I do not see Domains when I am viewing Users. How can I achieve this. ...

What is a sane way to perform a radical Django Model migration in a production environment?

I have an existing django web app that is in use. I have to radically migrate one key model in my design to a completely new design, but I want to cache all of the existing data for that model and migrate them to the new records in production when ready to deploy. I can afford to bring my website down for a few hours one night and do w...

How do I pass a list of Qs to filter for OR lookups?

How do I pass a list of Qs to filter for OR lookups? Something like: q_list = [Q(xyz__isnull=True), Q(x__startswith='x')]? Without a list I would do: Model.objects.filter(Q(xyz__isnull=True) | Q(x__startswith='x')) ...

How to write a Django view to obtain CharFields of multiple Django models that can only be accessed through relations

Please see the following Django models: - class Student(models.Model): reference_num = models.CharField(max_length=50, unique=True) name = models.CharField(max_length=50) birthdate = models.DateField(null=True, blank=True) is_active = models.BooleanField(db_index=True) class Examination(models.Model): short_name = ...

verbose_name_plural unexpected in a model?

Hi! I've been doing some models of a future app, and, after adding verbose_name and verbose_name_plural to every entry on a working model, for making it 'beautiful', I've found that at validate time, Django doesn't like that, so it says: File "/home/andor/Documentos/desarrollo/grundymanage/../grundymanage/concursantes/models.py", lin...

Django ORM: caching and manipulating ForeignKey objects

Consider the following skeleton of a models.py for a space conquest game: class Fleet(models.Model): game = models.ForeignKey(Game, related_name='planet_set') owner = models.ForeignKey(User, related_name='planet_set', null=True, blank=True) home = models.ForeignKey(Planet, related_name='departing_fleet_set') dest = model...

Django Imagefield not working properly via ModelForm

I'm certain I'm doing something really obviously stupid, but I've been trying to figure it out for a few hours now and nothing is jumping out at me. I'm using a ModelForm so I can expose a few fields from a model for editing. 2x ImageField, 1x TextField. The Form is processed and the TextField works. The two ImageFields do not work and ...

Nullable ForeignKeys and deleting a referenced model instance

I have a ForeignKey which can be null in my model to model a loose coupling between the models. It looks somewhat like that: class Message(models.Model): sender = models.ForeignKey(User, null=True, blank=True) sender_name = models.CharField(max_length=255) On save the senders name is written to the sender_name attribute. Now, I wa...

How do I do a not equal in Django queryset filtering?

In django model querysets, I see that there is a __gt and __lt for comparitive values, but is there a __ne/!=/<> (not equals?) I want to filter out using a not equals: Example: Model: bool a; int x; I want results = Model.objects.exclude(a=true, x!=5) The "!=" is not correct syntax. I tried __ne, <>. I ended up using: re...

How to retrieve objects with a foreign key self

I have a model Category which has a FK reference to itself. How can i send the data to a template and make it look like this Category 1 List item List item Category 2 List item List item ...

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

Django Model.object.get pre_save Function Weirdness

Hello! I have made a function that connects to a models 'pre_save' signal. Inside the function I am trying to check if the model instance's pk already exists in the table with: sender.objects.get(pk=instance._get_pk_val()) The first instance of the model raises an error. I catch the error and generate a slug field from the title. In a...

Django admin - inline inlines (or, three model editing at once)

Hi, I've got a set of models that look like this: class Page(models.Model): title = models.CharField(max_length=255) class LinkSection(models.Model): page = models.ForeignKey(Page) title = models.CharField(max_length=255) class Link(models.Model): linksection = models.ForeignKey(LinkSection) text = models.CharFiel...