django-models

Django. Apply function to queryset

眉间皱痕 提交于 2020-01-04 05:17:43
问题 I have a queryset and I want to apply function (port_to_app) to its field (dst_port). And then access it from template. How do I do that? I've read about filters and tags, but can't understand the way they work. models.py class FlowQuerySets(models.QuerySet): def top_app(self): return self.values('dst_port')\ .annotate(traffic=Sum('bytes')).order_by('-traffic')[:10] class Flow(models.Model): id = models.BigIntegerField(primary_key = True) ip_src = models.CharField(max_length=15) ip_dst =

How to populate html table with info from list in django

跟風遠走 提交于 2020-01-04 05:12:34
问题 I want to populate, in my django application, my table from base.html with the results from urlparse.py (this returns a list of 20 URLs from a site). models.py from django.db import models from django.utils.encoding import smart_unicode # Create your models here. urlparse.py import HTMLParser, urllib2 class MyHTMLParser(HTMLParser.HTMLParser): site_list = [] def reset(self): HTMLParser.HTMLParser.reset(self) self.in_a = False self.next_link_text_pair = None def handle_starttag(self, tag,

How to populate html table with info from list in django

南楼画角 提交于 2020-01-04 05:12:10
问题 I want to populate, in my django application, my table from base.html with the results from urlparse.py (this returns a list of 20 URLs from a site). models.py from django.db import models from django.utils.encoding import smart_unicode # Create your models here. urlparse.py import HTMLParser, urllib2 class MyHTMLParser(HTMLParser.HTMLParser): site_list = [] def reset(self): HTMLParser.HTMLParser.reset(self) self.in_a = False self.next_link_text_pair = None def handle_starttag(self, tag,

SQLAlchemy equivalent of Django ORM's relationship-spanning filter

主宰稳场 提交于 2020-01-04 03:17:13
问题 This example is from the Django documentation. Given the (Django) database model: class Blog(models.Model): name = models.CharField(max_length=100) class Entry(models.Model): blog = models.ForeignKey(Blog) headline = models.CharField(max_length=255) body_text = models.TextField() In Django I can use: Entry.objects.filter(blog__name__exact='Beatles Blog') to get all Entry objects for blogs with the specified name. Question : What is the equivalent SQLAlchemy statement, given the model

How to calculate sum and also cumulative sum in django orm

前提是你 提交于 2020-01-04 02:19:04
问题 I have table project, project has sub project and subproject has developers. Another table sprint, work distribute in sprint 1 2 ..n so on, Each sprint has different developers data. Now How can i calculate sum, current sprint value and percentage complete Using Django Orm. Project has sub project foreign key class project(models.Model): title = models.CharField(max_length=150) project = models.ForeignKey("self", on_delete=models.CASCADE, related_name="subproject", blank=True, null=True)

Django: file field validation in model using python-magic

好久不见. 提交于 2020-01-04 02:15:30
问题 I have a model containing file field. I want to restrict it to pdf files. I have written clean method in model because I want to check for admin and shell level model creation also. But it is not working in model clean method. However form clean method is working. class mymodel(models.Model): myfile = models.FileField() def clean(): mime = magic.from_buffer(self.myfile.read(), mime=True) print mime if not mime == 'application/pdf': raise ValidationError('File must be a PDF document') class

Django Multilingual Text Field using JSON

限于喜欢 提交于 2020-01-03 17:18:15
问题 I recently ask this question Custom Django MultilingualTextField model field but I found no good reason why I should not do this, so I create a model Field that support multilingual text, auto return text in current language. This basically is the field that store custom Language object to database in json format. Here is the code: Github: https://github.com/james4388/django-multilingualfield Ussage: from django.db import models from multilingualfield import MLTextField, MLHTMLField class

Django query model - GROUP BY, MIN, MAX

梦想与她 提交于 2020-01-03 16:57:21
问题 I'm having trouble transferring my query to django. In sqlite3 it looked like this: SELECT A, MIN(B), MAX(B) from table GROUP BY A This would output unique values from A with a range of values from B Any hints on how to approach this? Is it even possible in django? 回答1: You can use values() for the GROUP BY , and annotate() for the MIN and MAX : from django.db.models import Min, Max MyModel.objects.values('A').annotate(min_b=Min('B'), max_b=Max('B')) You'll get a list of dictionaries,

Django `bulk_create` with related objects

邮差的信 提交于 2020-01-03 16:47:50
问题 I have a Django system that runs billing for thousands of customers on a regular basis. Here are my models: class Invoice(models.Model): balance = models.DecimalField( max_digits=6, decimal_places=2, ) class Transaction(models.Model): amount = models.DecimalField( max_digits=6, decimal_places=2, ) invoice = models.ForeignKey( Invoice, on_delete=models.CASCADE, related_name='invoices', null=False ) When billing is run, thousands of invoices with tens of transactions each are created using

Django `bulk_create` with related objects

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-03 16:46:15
问题 I have a Django system that runs billing for thousands of customers on a regular basis. Here are my models: class Invoice(models.Model): balance = models.DecimalField( max_digits=6, decimal_places=2, ) class Transaction(models.Model): amount = models.DecimalField( max_digits=6, decimal_places=2, ) invoice = models.ForeignKey( Invoice, on_delete=models.CASCADE, related_name='invoices', null=False ) When billing is run, thousands of invoices with tens of transactions each are created using