django-models

Load CSS/images in Django

做~自己de王妃 提交于 2020-01-05 02:31:08
问题 I've read the official Django Docs and many other posts here in SO. Still I'm unable to figure out the way to load my CSS files in HTML pages. I'm running Django 1.4 . Here is my whole structure of mysite project having auth as the app. mysite/ manage.py settings.py __init__.py urls.py auth/ __init__.py forms.py models.py views.py templates/ index.html home.html media/ common.css How can I load my common.css file in index and home template files? 回答1: Take a look at static files : https:/

Is a reason I can't add a ManyToManyField?

▼魔方 西西 提交于 2020-01-04 15:33:14
问题 So I'm building a Django application, and these are a few models I have: class MagicType(models.Model): name = models.CharField(max_length=155) parent = models.ForeignKey('self', null=True, blank=True) class Spell(models.Model): name = models.CharField(max_length=250, db_index=True) magic_words = models.CharField(max_length=250, db_index=True) magic_types = models.ManyToManyField(MagicType) When syncing the models I'm getting this error: AttributeError: 'ManyToManyField' object has no

Sum computed column in Django QuerySet

拜拜、爱过 提交于 2020-01-04 14:29:09
问题 Given the following Contribution model: class Contribution(models.Model): start_time = models.DateTimeField() end_time = models.DateTimeField(null=True) is it possible, using the Django database API, to reproduce the following SQL statement? SELECT SUM(end_time - start_time) AS total_duration FROM contribution; I've figured out this much: Contribution.objects.aggregate(total_duration=models.Sum( ??? )) but I'm not sure about how to represent the end_time - start_time part. Thanks! 回答1: Not

Display forms choice in template-Django

一世执手 提交于 2020-01-04 14:08:30
问题 On the template, when I call person.health_issue, I am getting '1','2' instead of 'Abdominal pain','Anaphylaxis'. How to display the value ('Abdominal pain','Anaphylaxis') instead of the code(1 or2 etc). I tried with this also {{ person.get_health_issue_display }} in template,it is not displayed anything. forms.py HEALTH_USSUES = ( ('1', 'Abdominal pain'), ('2', 'Anaphylaxis'), ('3', 'Asthma'), ('4', 'Bruising'), ('5', 'Chest pains'), ('6', 'Coughs or Colds') ) class PersonActionsForm(forms

Foreign keys and IntegrityError: id may not be NULL

天涯浪子 提交于 2020-01-04 11:43:12
问题 Django newbie here. I have two simple models like this: class Fluff(models.Model): # .... # some fields definitions class Something(models.Model): name = models.CharField(max_length=200) fluff = models.ForeignKey(Fluff) syncdb goes as expected, and then I go to the shell to test things: >>> s = Something() >>> s.name = 'Blah' >>> s.fluff = Fluff(...) >>> s.save() Traceback (most recent call last): # traceback skipped for brevity IntegrityError: app_something.fluff_id may not be NULL I

Django, annotation and ordering using data from other table

强颜欢笑 提交于 2020-01-04 09:14:32
问题 In my app there are users who request items and users who donate those items. I would like to fetch all users that made the most donations. These are my models: class ItemRequest(models.Model): item_type = models.ForeignKey(ItemType) description = models.CharField(max_length=1024, default='', blank=True) quantity = models.IntegerField() user = models.ForeignKey(User) completed = models.BooleanField(default=False) completed_dt = models.DateTimeField() class Donation(models.Model): item_request

Passing parameter to upload_to function in Django

岁酱吖の 提交于 2020-01-04 06:13:14
问题 I try to upload multiple images of an instance to different sub folders. But I need to rename each uploaded file as well so I implement a function for upload_to field as below. class MyModel(models.Model): code = models.CharField() logo = models.FileField(upload_to=get_path) cover = models.FileField(upload_to=get_path) def get_path(instance, filename): ext = filename.split('.')[-1] new_name = "%s.%s" % (slughifi(filename), ext) return new_name However, I'm not sure how I can divide images to

Passing parameter to upload_to function in Django

流过昼夜 提交于 2020-01-04 06:08:10
问题 I try to upload multiple images of an instance to different sub folders. But I need to rename each uploaded file as well so I implement a function for upload_to field as below. class MyModel(models.Model): code = models.CharField() logo = models.FileField(upload_to=get_path) cover = models.FileField(upload_to=get_path) def get_path(instance, filename): ext = filename.split('.')[-1] new_name = "%s.%s" % (slughifi(filename), ext) return new_name However, I'm not sure how I can divide images to

Updating many objects as result of Celery task: Django post_save or 2nd Celery task?

你离开我真会死。 提交于 2020-01-04 05:59:11
问题 I have a Celery task that runs periodically and may update a few objects on each run (order of magnitude ~10 would be the most per run). I have 2 other steps that need to occur if models are updated by that Celery task, and had planned to use the Django Model post_save signal to accomplish them. However, I realized that handing update of a large number of objects (potentially tens or hundreds of thousands) in a post_save handler might not be ideal. Here's the flow of what I'm trying to

How can I do INNER JOIN in Django in legacy database?

半腔热情 提交于 2020-01-04 05:23:32
问题 Sorry for probably simple question but I'm a newby in Django and really confused. I have an ugly legacy tables that I can not change. It has 2 tables: class Salespersons(models.Model): id = models.IntegerField(unique=True, primary_key=True) xsin = models.IntegerField() name = models.CharField(max_length=200) surname = models.CharField(max_length=200) class Store(models.Model): id = models.IntegerField(unique=True, primary_key=True) xsin = models.IntegerField() brand = models.CharField(max