django-models

django tutorial 3 indexpage missing

人走茶凉 提交于 2020-01-17 15:48:45
问题 i am learning django by using django 1.6 documents tutorial 1 - 6. this round is my 4th try and, previous 3 try was successful and i understand more on every try. i am in tutorial 3 now, to create views. according to the documents, after i created a view, i need to map it to a URL. so i follow the documents to add a urls.py in the polls directory. and then i follow the document to add include() to mysite/urls.py i am able to so called wired an index view into the polls view. now if i go back

Django model get unique foreign key rows based on other column max value

独自空忆成欢 提交于 2020-01-17 12:40:06
问题 I'm new to Django, and I'm trying to create a simple application that keep track on different server configurations in a SQlite database. I've created 2 database models: from django.db import models class Server(models.Model): name = models.CharField(max_length=250) class Config(models.Model): server = models.ForeignKey(Server, on_delete=models.CASCADE) configuration = models.CharField(max_length=250) config_version = models.IntegerField() Here are the 2 models sample data: Server: | id |

Django 1.8: How do I use a model in a different app as a 'through' in a Many to Many relationship?

南笙酒味 提交于 2020-01-17 08:26:51
问题 The docs only show using a many-to-many-through/intermediary model where the model is in the same app. How can I do this with the intermediary table in a different app? I don't think from app import intermediary works, as the through attribute is declared within apostrophes. 回答1: Answer: Use 'app.model' . I didn't think it would work in this context, but it does 来源: https://stackoverflow.com/questions/31746235/django-1-8-how-do-i-use-a-model-in-a-different-app-as-a-through-in-a-many-to

Save date in django in desired format

泪湿孤枕 提交于 2020-01-17 07:53:11
问题 I want to save date in my django model into desired format. post_date = str(request.POST['date']) I am getting date from front end is 12/21/2016 i want to save this date in my django modal is like 21-Dec-2016 . How can i do it. it will be good if you can provide some sample code. Thanks in advance. 回答1: you want to store in DB string parameter? The easiest way convert input string to date and then convert from date to string. But of course it is more logical to store date format imho 来源:

Should I use ArrayField or ManyToManyField for tags

青春壹個敷衍的年華 提交于 2020-01-17 06:42:13
问题 I am trying to add tags to a model for a postgres db in django and I found two solutions: using foreign keys: class Post(models.Model): tags = models.ManyToManyField('tags') ... class Tag(models.Model): name = models.CharField(max_length=140) using array field: from django.contrib.postgres.fields import ArrayField class Post(models.Model): tags = ArrayField(models.CharField(max_length=140)) ... assuming that I don't care about supporting other database-backends in my code, what is a

Django - How to use a filter with a foreign key field?

[亡魂溺海] 提交于 2020-01-17 04:56:06
问题 I'm trying to do this: LogEntry.objects.filter(content_type='visitor') Where LogEntry is my model and content_type is a ForeignKey field pointing to another table with field id, and content_type (varchar). How can I search by the value of the other table? When I try to run the above it says: invalid literal for int() with base 10: 'visitor' 回答1: Ahh, found it in another SO answer. Weird it didn't seem to be documented. Or I just skimmed over it. Answer: LogEntry.objects.filter(content_type_

Lookup hour on DateTimeField Django

跟風遠走 提交于 2020-01-17 04:39:05
问题 I have the model class Item(models.Model): inicio = models.DateTimeField() When I try to make this query: itens = Item.objects.filter(inicio__hour__gte=6) It returns me: FieldError Unsupported lookup 'hour' for DateTimeField or join on the field not permitted. How can I make this query? 回答1: You need to filter using a timedelta: from datetime import datetime, timedelta five_hours_ago = datetime.now() - timedelta(hours=5) items = Item.objects.filter(inicio__lt=five_hours_ago) You can always

Django django.db.migrations.graph.CircularDependencyError

夙愿已清 提交于 2020-01-17 04:08:27
问题 I am facing circulation error while migrating my app. when i run this: (virEnv)abc@abc-All-Series:~/vissa_poc$ python manage.py migrate forms **I get this:** Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/home/abc/virEnv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line utility.execute() File "/home/abc/virEnv/local/lib/python2.7/site-packages/django/core

How to import postgres relations into django 1.8

泪湿孤枕 提交于 2020-01-17 01:37:14
问题 I have an existing django database in postgres and it has several relationships. I have a new model in django postgres and I want to import some of the tables from existing database which have relationships. Do I need to map old fields to the new model? How do I preserve relationships? Is there a tool that helps in the process? Thanks 回答1: use python manage.py inspectdb It will create models from your legacy database. Read more here: https://docs.djangoproject.com/en/1.8/howto/legacy

Django Model Field with Secondary Field

不想你离开。 提交于 2020-01-17 01:25:09
问题 I am trying to create a user sign up form in Django that allows for a primary field with an associated integer field. I need each user to be able to select a genre (for movies), and then the percentage (0-100) they like that genre. I have created the percentage as a separate model, but I need it to be associated with each genre per user. How can I associate each user's genres with a specific "like" percentage? Right now, I just have a box of the genre list, with no way to select the