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 ...
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):`
-----------
...
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 ...
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...
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...
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 ...
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...
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)
...
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...
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...
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)?
...
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...
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 ...
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...
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 ...
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...
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?
...
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...
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...
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)....