django-models

Database first Django models

血红的双手。 提交于 2021-01-27 06:05:30
问题 In ASP.NET there is entity framework or something called code first from database. Is there something similar for Django? I usually work with a pre-existing database that I need to create a backend (and subsequently a front end) for. Some of these RDBs have many tables and relations so manually writing models isn't a good idea. I've scoured Google for solutions but have come up relatively empty handed. Thoughts would be appreciated. Thanks! 回答1: You can use the information on this link.

Django exact match of ForeignKey Values

时间秒杀一切 提交于 2021-01-27 03:51:00
问题 class Sentence(Model): name = CharField() class Tokens(Model): token = CharField() sentence = ForeignKey(Sentence, related_name='tokens') I want to implement two cases: Sentence consists exactly of three tokens ['I', 'like', 'apples'] . So list of sentence.tokens.all() is exactly ['I', 'like', 'apples'] . Same as above, but contains tokens (part of sentence). Sentence.objects.annotate(n=Count('tokens',distinct=True)).filter(n=3).filter(tokens__name='I').filter(tokens__name='like').filter

Django exact match of ForeignKey Values

余生长醉 提交于 2021-01-27 03:50:48
问题 class Sentence(Model): name = CharField() class Tokens(Model): token = CharField() sentence = ForeignKey(Sentence, related_name='tokens') I want to implement two cases: Sentence consists exactly of three tokens ['I', 'like', 'apples'] . So list of sentence.tokens.all() is exactly ['I', 'like', 'apples'] . Same as above, but contains tokens (part of sentence). Sentence.objects.annotate(n=Count('tokens',distinct=True)).filter(n=3).filter(tokens__name='I').filter(tokens__name='like').filter

How to delete an ImageField image in a Django model

十年热恋 提交于 2021-01-22 05:45:56
问题 I have a Profile model like so: class Profile(models.Model): photo = models.ImageField(upload_to="img/users/", blank=True) I want my users to be able to remove their profile pictures so that there's no image in photo i.e blank. I tried Profile.objects.get(id=1).photo.delete(save=False) which deleted the image but the url was still there so I was getting a 404. How do I clear the image and url? EDIT: In my template, I'm like: {% if profile.photo %} <img src="{{ MEDIA_URL }}{{ profile.photo }}"

Using select_related to get related_name objects

故事扮演 提交于 2021-01-21 10:57:13
问题 I've been searching for a while now for a solution to my problem, but I can't seem to figure this out. So I have two models: class Messages(models.Model): _db = 'somedb' id = models.IntegerField(primary_key=True) text = models.TextField() class MessageTags(models.Model): _db = 'somedb' id = models.IntegerField(primary_key=True) text = models.TextField() tag = models.ForeignKey(Tag) message = models.ForeignKey(Messages, related_name='tags') I am able to query the tags for a given message using

Which Django Form Field can provide me a HTML output of <input type=“range” />?

Deadly 提交于 2021-01-21 09:49:05
问题 I've got a html form with this input: <input id="duration_slider" type="range" min="1" max="168" value="48" /> However I'd now like to convert this slider to a django form field, so I can use it in a Django form. The field name is duration so I'll change the id to id=id_duration . What would my models.py and forms.py looks like for this field? Currently it's just: class AdvertisePost(Post): ... duration = models.IntegerField(default=48) def __str__(self): return self.title class

Annotate with latest related object in Django

徘徊边缘 提交于 2021-01-21 07:01:36
问题 I have a model Conversation and a model Message . The model Message has a foreign key to conversation, a text field, and a date field. How can I list all conversations and for each conversation get the most recent message and the date of the most recent message? I guess it's something like Conversation.objects.annotate(last_message=Max('messages__date')) but it will only give me the latest date. I want last_message to contain both the text of the last message and the date it was created.

Annotate with latest related object in Django

梦想的初衷 提交于 2021-01-21 07:01:06
问题 I have a model Conversation and a model Message . The model Message has a foreign key to conversation, a text field, and a date field. How can I list all conversations and for each conversation get the most recent message and the date of the most recent message? I guess it's something like Conversation.objects.annotate(last_message=Max('messages__date')) but it will only give me the latest date. I want last_message to contain both the text of the last message and the date it was created.

convert a django queryset into an array

放肆的年华 提交于 2021-01-21 06:30:30
问题 i would like convert a django queryset into an array like, firstnames=Users.objects.values('firstnames') to get a result that looks like firstnames = ["Nancy", "Andrew", "Janet", "Margaret", "Steven", "Michael", "Robert", "Laura", "Anne"]; Any insights please? Regards Josh 回答1: Use QuerySet.values_list and specify flat=True : firstnames = Users.objects.values_list('firstnames', flat=True) firstnames = list(firstnames) 回答2: def get_array(Table, column): rows = Table.objects.values(column)

Django Full Text Search Optimization - Postgres

五迷三道 提交于 2021-01-21 04:30:11
问题 I am trying to create a Full Text Search for an address autocomplete feature, leveraging Django (v2.1) and Postgres (9.5), but the performance is not suitable for an auto-complete at the moment and I don't get the logic behind the performance results I get. For info the table is quite sizeable, with 14 million rows. My model: from django.db import models from postgres_copy import CopyManager from django.contrib.postgres.indexes import GinIndex class Addresses(models.Model): date_update =