django-models

possible bug in django models

models.py: class root(models.Model): uid_string = models.CharField(max_length=255, unique=True) class tree(models.Model): uid_string = models.ForeignKey(root, to_field='uid_string', db_column='uid_string') class shrub(models.Model): uid_string = models.ForeignKey(root, to_field='uid_string') obviously, the column ...

How to delete a record in django models.

I want to delete a record. But I of a particular record. Such as delete from table_name where id = 1; How can I do this in a django model? I am writing a function: def delete(id):` ----------- ...

How to get field with ForeignKey('self') without the possibilty to link to same entry?

Hallo Folks, I hope you can help me with a little problem I came in touch with while building a model with a foreign key to itself. Here an example: class Example (model.Model): parent = models.ForeignKey('self', null=True, blank=True) # and some other fields After creating a new entry in the admin panel and going into this ...

Django notification comment get owner

I am looking at accessing the user who owns the content_type of a posted comment Currently I can access the user who posts, the comment, however I would like to notify the person who owns the item... I tried doing user = comment.content_type.user but I get an error. In my main __init__.py file As soon as I change that to user = reque...

Which is the easiest way to remove Django User model's 30 character limit?

I've inherited a Django application which uses a custom form-less auth backend. It works around the 30 character limit in django.contrib.auth.models.User by a SQL hack ALTER TABLE auth_user MODIFY COLUMN username varchar(90);, which is of course only valid for the database, but not for forms. Since I'm trying to remove all the hacks from...

order objects by most unique views (stored in another table with ip) in django

Related models: models.py class Entry(models.Model): ... class EntryView(models.Model): entry = models.ForeignKey(Entry) dt_clicked = models.DateTimeField(auto_now_add=True) ip = models.CharField(max_length=15, db_index=True, blank=True) host = models.CharField(max_length=64, db_index=True, blank=True) referer ...

Quick counting with linked Django Models

I've got a stack of Models something like this (I'm typing it out in relative shorthand): class User: pass class ItemList: pass # A User can have more than one ItemList # An ItemList can have more than one User # Classic M2M class ItemListOwnership: user = fk(User) itemlist = fk(ItemList) # An ItemList has multipl...

How to create a unique slug in Django

I am trying to create a unique slug in Django so that I can access a post via a url like this: http://www.mysite.com/buy-a-new-bike_Boston-MA-02111_2 The relevant models: class ZipCode(models.Model): zipcode = models.CharField(max_length=5) city = models.CharField(max_length=64) statecode = models.CharField(max_length=32) ...

Annotating django QuerySet with values from related table

I have a model Page with 2 related models PageScoreHistory and PageImageHistory, where both history models have page field as a ForeignKey to Page and have a date field. The problem I am having, is that when I get a list of PageImageHistory QuerySet, I want to be able to retrieve the score the page had at the time the image was taken. Fo...

Django - populate a MultipleChoiceField via request.user.somechoice_set.all()

Is there a more efficient, or cleaner way to do the following? class SpamForm(ModelForm): some_choices = fields.MultipleChoiceField() def __init__(self, *args, **kwargs): super(SpamForm, self).__init__(*args, **kwargs) self.fields['some_choices'].choices = [[choice.pk, choice.description] for choice in self.inst...

Distinct in many-to-many relation

class Order(models.Model): ... class OrderItem(models.Model) order = models.ForeignKey(Order) product = models.ForeignKey(Product) quantity = models.PositiveIntegerField() What I need to do is to get the Order(s) which has only one order item. How can I write it using the QuerySet objects (without writing SQL)? ...

How to perform this sql in django model?

SELECT *, SUM( cardtype.price - cardtype.cost ) AS profit FROM user LEFT OUTER JOIN card ON ( user.id = card.buyer_id ) LEFT OUTER JOIN cardtype ON ( card.cardtype_id = cardtype.id ) GROUP BY user.id ORDER BY profit DESC I tried this: User.objects.extra(select=dict(profit='SUM(cardtype.price-cardtype.cost)')).annotat...

A m2m Django permission model

Users on a webapp I'm building have multiple objects that are "theirs" Let's pretend the object is called Toy. I want them to be able to set privacy options for their Toys so they can set the following visibility options: Friends of friends Friends Only allow a defined set of people Friends only, but deny a set of people (to keep it ...

Can you access a Django Model "property" from it's ModelForm?

I have a Django model class with a non-model-field property, ex: def _get(self): return "something" description = property(_get) I'm using the model class with in a ModelForm / ModelFormset. Is there any way to access the property from the form / formset? If not, what's best practice for including extra "display" data in a djang...

Django Advantage forms.Form vs forms.ModelForm

There is a question very similar to this but I wanted to ask it in a different way. I am a very customized guy, but I do like to take shortcuts at times. So here it goes. I do find these two classes very similar although one "helps" the programmer to write code faster or have less code/repeating code. Connecting Models to Forms sounds ...

django manytomany through

If I have two Models that have a manytomany relationship with a through model, how do I get data from that 'through' table. class Bike(models.Model): nickname = models.CharField(max_length=40) users = models.ManyToManyField(User, through='bike.BikeUser') The BikeUser class class BikeUser(models.Model): bike = models.F...

Django - can you make a subclass of a model just to alter behavior?

I'm looking to do the opposite of what Django's proxy model does. I want to subclass Model, add some extra methods to it, add behavior to save(), set a default manager that adds some my-application-specific methods, and then subclass that to create most of the models in my application. Is this possible? ...

How to return multiple objects related with ForeignKey in Django

I have the following in my models.py: class HostData(models.Model): Manager = models.ForeignKey(Managers) Host = models.CharField(max_length=50, null=True) HostStatus = models.CharField(max_length=200, null=True) Cpu = models.PositiveIntegerField(max_length=10, null=True) Disk = models.FloatField(null=True) I would like to r...

Django ManyToMany in one query

Hi, I'm trying to optimise my app by keeping the number of queries to a minimum... I've noticed I'm getting a lot of extra queries when doing something like this: class Category(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=127, blank=False) class Project(models.Model): categorie...

Django : load a restricted set of fields of objects loaded using a foreign key

I have the following code, using Django ORM routes =Routes.objects.filter(scheduleid=schedule.id).only('externalid') t_list = [(route.externalid, route.vehicle.name) for route in routes]) and it is very slow, because the vehicle objects are huge (dozens of fields, and I cannot change that, it is coming from a legacy database)....