django-models

Creating a model object with a M2M and FK relationship

不羁岁月 提交于 2020-01-25 06:42:27
问题 I have the following model -- class Category(models.Model): category = models.CharField(max_length=100) class Credit(models.Model): person = models.CharField(max_length=100) position = models.CharField(max_length=100) class Video(models.Model): title = models.CharField(max_length=100) category = models.ForeignKey(Category) credits = models.ManyToManyField(Credit) I have created the following Category and Credit objects -- >>> Category.objects.create(category='animation') >>> Category.objects

Relation does not exist - Django & Postgres

心已入冬 提交于 2020-01-25 05:21:05
问题 I added some table to an existant model, but I got an error when i push my project to heroku : This happens to any models I add on this particular app ProgrammingError at /admin/dashboard/adressbook/ relation "dashboard_adressbook" does not exist LINE 1: SELECT COUNT(*) AS "__count" FROM "dashboard_adressbook" My models.py class AdressBook(models.Model): author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="Nom de l'utilisateur") text = models.CharField(max_length=200

Django: optimizing a query with spread data

青春壹個敷衍的年華 提交于 2020-01-25 02:24:15
问题 I have Order objects and OrderOperation objects that represent an action on a Order (creation, modification, cancellation). Conceptually, an order has 1 to many order operations. Each time there is an operation on the order, the total is computed in this operation. Which means when I need to find the total of an order, I just get the last order operation total. The simplified code class OrderOperation(models.Model): order = models.ForeignKey(Order) total = DecimalField(max_digits=9, decimal

Local field clashes with field of similar name

拈花ヽ惹草 提交于 2020-01-25 00:45:07
问题 I'm trying to add a new database model that will let me "group" expenses, but am running across this issue when running python manage.py makemigrations My virtual environment looks like this: Django==1.7.3 argparse==1.2.1 django-braces==1.4.0 django-chartit==0.1 django-crispy-forms==1.4.0 django-debug-toolbar==1.2.2 psycopg2==2.6 six==1.9.0 sqlparse==0.1.14 wsgiref==0.1.2 Here is the traceback: Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line

DISTINCT ON fields is not supported by this database backend

无人久伴 提交于 2020-01-24 23:32:47
问题 I am using distinct to get the distinct latest values but it is giving me an error: DISTINCT ON fields is not supported by this database backend views.py class ReportView(LoginRequiredMixin, generic.TemplateView): template_name = 'admin/clock/report.html' def get_context_data(self, **kwargs): context = super(ReportView, self).get_context_data(**kwargs) context['reports'] = TimesheetEntry.objects.filter( timesheet_jobs__job_company = self.request.user.userprofile.user_company, ).distinct(

Django two table relationship, but not both

送分小仙女□ 提交于 2020-01-24 20:32:08
问题 I have a table called Post. A post can have 2 videos or 2 images, but not both. The table schema for a post looks like this: class Post(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) header = models.CharField() created_at = models.DateTimeField(auto_now_add=True) I have two tables that look similar to each other: class PostImage(models.Model): post = models.ForeignKey(Post, on_delete=models.CASCADE) img = models.ImageField() class PostVideo(models.Model): post =

Django model update or create object with unique constraint

时光毁灭记忆、已成空白 提交于 2020-01-24 16:50:51
问题 There is a model: class Proxy(models.Model): host = models.CharField(max_length=100,) port = models.CharField(max_length=10,) login = models.CharField(max_length=100,) password = models.CharField(max_length=100,) class Meta: unique_together = ("host", "port") I added batch of proxies in admin interface, and one of them is 0.0.0.0:0000, login=123, password=123. Then I add another batch of proxy, and one of them is the same 0.0.0.0:0000, but with new login=234 and password=234. Is any

Django model update or create object with unique constraint

岁酱吖の 提交于 2020-01-24 16:50:37
问题 There is a model: class Proxy(models.Model): host = models.CharField(max_length=100,) port = models.CharField(max_length=10,) login = models.CharField(max_length=100,) password = models.CharField(max_length=100,) class Meta: unique_together = ("host", "port") I added batch of proxies in admin interface, and one of them is 0.0.0.0:0000, login=123, password=123. Then I add another batch of proxy, and one of them is the same 0.0.0.0:0000, but with new login=234 and password=234. Is any

Django Select Query Time Diff

风流意气都作罢 提交于 2020-01-24 15:14:45
问题 I am trying to query a database table in django with, among others, the following columns: id | start_time | end_time Rather than getting the separate values for the two, can I just get the difference directly in a query? Something to this effect: SELECT id, Diff(start_time, end_time) FROM myTable 回答1: QuerySet.extra() will allow you to specify arbitrary expressions for a column. Note that the result will be DB-dependent. 回答2: This can be done entirely through the ORM in Django 1.10 and above

django orm: select_related, fooling reverse foreign key with a fake foreign key addition to the model, what can go wrong?

这一生的挚爱 提交于 2020-01-24 15:06:13
问题 I am trying to learn how to use Django's ORM for more advanced queries, instead of using raw sql. select_related makes joins to reduce database hits, in principle it could make the joins that I would do manually. But there is a problem: It doesn't use reverse foreign key relationships to make the sql. For the schema I have this is a nuisance. I have found what looks like a surprisingly easy work around, and I'm worried that it will be incorrect in ways I don't understand. I'm using a legacy