django-models

Use of meta class in django

99封情书 提交于 2020-04-05 19:11:22
问题 Can someone please explain why is meta class used in the following example. Ex: Class Employee (models.Model): name = models.ForeignKey(name) Gender = models.IntegerField() class Meta: ordering = ["Gender"] Thanks. 回答1: Because author/programmer wants to sort results by value of Gender field. 回答2: Django models use the Meta class to contain extra information about the model that would not necessarily be appropriate to contain within the model class itself. Note that this is not the same as

Parsing static json file and save values to Django Database/model

六月ゝ 毕业季﹏ 提交于 2020-03-26 03:55:06
问题 I have json file in my static folder in my django project. And I want to save the data from json file to my Django Database. This is my model.py from django.db import models class Coming_Soon(models.Model): movie_id = models.CharField(primary_key=True, max_length=11) movie_title = models.TextField(blank=True, max_length=100) image_url = models.TextField(blank=True) synopsis = models.TextField(blank=True) rating = models.TextField(default="MTRCB rating not yet available") cast = models

Django dynamic FileField upload_to

烂漫一生 提交于 2020-03-25 18:23:14
问题 I'm trying to make dynamic upload path to FileField model. So when user uploads a file, Django stores it to my computer /media/(username)/(path_to_a_file)/(filename). E.g. /media/Michael/Homeworks/Math/Week_1/questions.pdf or /media/Ernie/Fishing/Atlantic_ocean/Good_fishing_spots.txt VIEWS @login_required def add_file(request, **kwargs): if request.method == 'POST': form = AddFile(request.POST, request.FILES) if form.is_valid(): post = form.save(commit=False) post.author = request.user post

How to append values in a tabular format from JSON in Django Template?

此生再无相见时 提交于 2020-03-25 13:48:44
问题 I am making a small web application that allow user to perform OCR on the selected image and provides the JSON output in a tabular Format like this. JSON DATA : { "data": { "test_1": { "test_name": "FASTING SUGAR", "results": "121.00", "units": "mg%", "low_range": 70.0, "high_range": 110.0 } } } What I'm facing now is when I'm selected new image to add values in the table, it overrides the values instead of appending. And what I want is to append new value in the table by choosing another

Django cannot create or render comments

ε祈祈猫儿з 提交于 2020-03-24 00:16:46
问题 Hi have this structure to handle users posts and comments my post model is from django.db import models from django.contrib.auth.models import User class Post(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, default=0) profile = models.ForeignKey('users.Profiles', on_delete=models.CASCADE, null=True, default=1, blank=True) title = models.CharField(max_length=100, blank=True, null=True) desc = models.CharField(max_length=1000, blank=True, null=True) photo = models

Django Unable to rollback with try-exception block for atomic transactions

别说谁变了你拦得住时间么 提交于 2020-03-23 08:00:20
问题 One of my view in Django executes save operations on 6-7 tables. I want these transactions to be atomic I,e if the 5th or 6th transaction fail I want to rollback all the previous saves. The view contains a try-except block to handle the exceptions raised. It looks something like this: @transaction.atomic def my_view(request): sid = transaction.savepoint() try: Table1.save() Table2.save() Table3.save() Table4.save() Table5.save() Table6.save() Table7.save() # This might fail. In case of

Django Count of Items in a Field

痞子三分冷 提交于 2020-03-19 05:05:25
问题 models.py class Event(models.Model): name = models.CharField(max_length=20, unique=True) distance = models.IntegerField() date = models.DateField() class Category(models.Model): name = models.CharField(max_length=20, unique=True) description = models.CharField(max_length=20, unique=True) isnew = models.BooleanField(default=False) class Result(models.Model): event = models.ForeignKey(Event) category = models.ForeignKey(Category) score = models.IntegerField() I want to do a query to return a

Django Count of Items in a Field

妖精的绣舞 提交于 2020-03-19 05:04:52
问题 models.py class Event(models.Model): name = models.CharField(max_length=20, unique=True) distance = models.IntegerField() date = models.DateField() class Category(models.Model): name = models.CharField(max_length=20, unique=True) description = models.CharField(max_length=20, unique=True) isnew = models.BooleanField(default=False) class Result(models.Model): event = models.ForeignKey(Event) category = models.ForeignKey(Category) score = models.IntegerField() I want to do a query to return a

Use multiple databases in Django with only one table “django_migrations”

佐手、 提交于 2020-03-18 11:02:40
问题 For a project in Django I have to use two databases: default and remote . I have created routers.py and everything works fine. There was a requirement to create a table on the remote database and I created migration, run it and the table django_migrations was created. I want to have only one table django_migrations , in the default database. The relevant part of routers.py is here: class MyRouter(object): # ... def allow_migrate(self, db, app_label, model_name=None, **hints): if app_label ==

Displaying both sides of a ManyToMany relationship in Django admin

旧巷老猫 提交于 2020-03-18 06:05:29
问题 Say I have the following models that have a many-to-many relationship: models.py: class Foo(models.Model): name = models.TextField() class Bar(models.Model): name = models.TextField() foos = models.ManyToManyField(Foo, related_name='bars') And then having defined them in admin in the following way: admin.py @admin.register(Foo) class FooAdmin(admin.ModelAdmin): """Foo admin.""" list_display = ('name',) search_fields = ('name',) @admin.register(Bar) class BarAdmin(admin.ModelAdmin): """Bar