django-2.1

Highlighted post in Django templates

你说的曾经没有我的故事 提交于 2020-02-05 03:30:49
问题 I've created this model: class PostModel(models.Model): post_title = models.CharField(max_length=70) post_short_description = models.TextField(max_length=200) post_contents = models.TextField() post_publishing_date = models.DateTimeField(auto_now=False, auto_now_add=True) post_author = models.ForeignKey(AuthorModel, on_delete=models.CASCADE) post_keyconcept = models.ManyToManyField(KeyConceptModel) slug = models.SlugField(verbose_name="Slug", unique="True") post_highlighted = models

Return ManyToManyField IDs when querying model in Django 2.1?

我怕爱的太早我们不能终老 提交于 2019-12-25 02:28:48
问题 I have two very simple models: class Person(models.Model): full_name = models.CharField(max_length=120) class Event(models.Model): event_date = models.DateField() short_description = models.CharField(max_length=250) people_involved = models.ManyToManyField( Person, blank=True, related_name="people_involved" ) I want to return all events, including the people_involved . In views.py, I have the following: def alljson(request): events = Event.objects.values() return JsonResponse(list(events),

Adding indexes to model fields in Django with migrations

十年热恋 提交于 2019-12-23 07:20:09
问题 I am trying to add indexes on model fields using Field.db_index for an app that has migrations. Looking at Django's documentation all I need to do is to set db_index=True : class Person(models.Model): first_name = models.CharField() last_name = models.CharField(db_index=True) and then I first tried the new Django's Migration: ./manage.py makemigrations app-name but Migration does not seem to notice the change and does not add the sql command for creating an index. So I tried django-admin.py

How to create a field in django model using values of other fields of same model?

心不动则不痛 提交于 2019-12-20 07:43:19
问题 I wants to create a field name: total which have the total price of all the products*quqantity. What I want is how can I do total = price*quantity in django model. As you can see there can be more than one products. I have join the OderItem model with Order through tabularinline. class OrderItemInline(admin.TabularInline): model = OrderItem raw_id_fields = ['product'] class OrderAdmin(admin.ModelAdmin): list_display = ['id','name', 'mobile_no','address','status', 'created'] list_editable = [

Is it possible to add 2nd slug to URL path in Django?

拥有回忆 提交于 2019-12-11 06:34:14
问题 I'm using Django version 2.1. I want to create this type of URL Path in my Project: www.example.com/bachelor/germany/university-of-frankfurt/corporate-finance Is it possible to do it in Django? 回答1: Yes, say for example that you have a slug for an Author , and one for a Book , you can define it as: # app/urls.py from django.urls import path from app.views import book_details urlpatterns = [ path('book/<slug: author_slug >/<slug: book_slug >/', book_details), ] Then the view looks like: # app

django.db.utils.IntegrityError: FOREIGN KEY constraint failed while executing LiveServerTestCases through Selenium and Python Django

天大地大妈咪最大 提交于 2019-12-10 09:24:02
问题 I can run all unit tests successfully, I can even run selenium tests successfully if I run an independent server, but when I try to use LiveServerTestCases to test everything in a self-contained manner, each LiveServerTestCase test ends with the following error after completing the tearDown function: File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\db\backends\base\base.py", line 239, in _commit return self.connection.commit() django.db.utils.IntegrityError: FOREIGN KEY

Combining more than one slug in the url

自古美人都是妖i 提交于 2019-12-08 05:14:00
问题 I'm trying to use two slug for generate the urls of a type of post. My site is divided in category and everyone of these have one or more post. views.py def singlePost(request, slug_post): blogpost = get_object_or_404(BlogPost, slug_post=slug_post) context = {"blogpost": blogpost} return render(request, "blog/single_post.html", context) def singleCategory_postList(request, slug_category): category = get_object_or_404(Category, slug_category=slug_category) blogpost = BlogPost.objects.filter

django.db.utils.IntegrityError: FOREIGN KEY constraint failed while executing LiveServerTestCases through Selenium and Python Django

落爺英雄遲暮 提交于 2019-12-05 17:46:09
I can run all unit tests successfully, I can even run selenium tests successfully if I run an independent server, but when I try to use LiveServerTestCases to test everything in a self-contained manner, each LiveServerTestCase test ends with the following error after completing the tearDown function: File "C:\Users\Win7\.virtualenvs\lang-QbOXb8q_\lib\site-packages\django\db\backends\base\base.py", line 239, in _commit return self.connection.commit() django.db.utils.IntegrityError: FOREIGN KEY constraint failed The above exception was the direct cause of the following exception: Traceback (most

How to create a field in django model using values of other fields of same model?

被刻印的时光 ゝ 提交于 2019-12-02 13:18:13
I wants to create a field name: total which have the total price of all the products*quqantity. What I want is how can I do total = price*quantity in django model. As you can see there can be more than one products. I have join the OderItem model with Order through tabularinline. class OrderItemInline(admin.TabularInline): model = OrderItem raw_id_fields = ['product'] class OrderAdmin(admin.ModelAdmin): list_display = ['id','name', 'mobile_no','address','status', 'created'] list_editable = ['status'] list_per_page = 15 list_filter = ['status'] search_fields = ('id',) inlines = [OrderItemInline

Django: TypeError: '<' not supported between instances (model objects)

我只是一个虾纸丫 提交于 2019-12-01 11:55:44
I'm trying to migrate my Django project from Python 2.7/Django 1.11 to Python 3.7/Django 2.1. I've found one issue and I want to understand its cause. I have 3 models in my project: class DeviceModel(models.Model): name = models.CharField(max_length=255) pirsh = models.CharField(max_length=255) def __str__(self): return self.name + " - " + self.pirsh class Device(models.Model): created_at = models.DateTimeField(auto_now_add=True) device_model = models.ForeignKey(DeviceModel, on_delete=models.CASCADE) serial_number = models.CharField(max_length=255) def __str__(self): return self.device_model