django-models

In a django custom field, what does the SubfieldBase metaclass do?

[亡魂溺海] 提交于 2020-02-04 09:26:39
问题 When you implement a custom Django field, you're supposed to include this line: __metaclass__ = models.SubfieldBase in the class definition, as in: class SomeCustomField(models.Field): __metaclass__ = models.SubfieldBase What does that actually do? Update: This is the source code for the metaclass (from the Django project): class SubfieldBase(type): """ A metaclass for custom Field subclasses. This ensures the model's attribute has the descriptor protocol attached to it. """ def __new__(cls,

Django : One model has two foreign keys is this the right approach in this case

痞子三分冷 提交于 2020-02-04 05:27:09
问题 I am currently learning about Django Models. Here is my current situation. I have three models 1-Patient_Names 2-Patient_Responses 3-Doctor_Questions Now here is the relationship there will be multiple patients which will be represented by the model Patient_Names . Now each patient will have specific responses to the questions asked by a doctor these responses are represented by the model Patient_Responses . Because of this the Patient_Responses model will have a field that is a foreignKey to

redefine default filtering behavior in Django templates

泄露秘密 提交于 2020-02-03 12:44:46
问题 I have a project with many DecimalFields that are rendered in more than 300 templates. I would like that these decimal fields are rendered normalized. I don't care about precission or anything: decimal.Decimal("10.0000").normalize() I haven't found a way to change default rendering system. I know there is a humanize and a floatformat filter I could use in my templates. But I need a solution that doesn't mean editing all those files, even if a shell script could be written. Thanks 回答1: You

React JSX and Django reverse URL

北城余情 提交于 2020-02-02 11:16:11
问题 I'm trying to build some menu using React and need some Django reverse urls in this menu. Is it possible to get django url tag inside JSX? How can this be used? render: function() { return <div> <ul className={"myClassName"}> <li><a href="{% url 'my_revese_url' %}">Menu item</a></li> </ul> </div>; } 回答1: You could create a script tag in your page to inject the values from Django into an array. <script> var menuItems = [ {title: 'Menu Item', url: '{% url "my_reverse_url" %}'}, {title: 'Another

Customize the djoser create user endpoint

一个人想着一个人 提交于 2020-02-01 19:02:27
问题 I am using djoser for auth purposes. I want to customize the create user end point of djoser. I have a User app. Here is my User model from django.db import models class User(models.Model): email = models.CharField(max_length=100, blank=False) name = models.CharField(max_length=100, blank=False) last_name = models.CharField(max_length=100, blank=False) account_address = models.CharField(max_length=30, blank=False) password = models.CharField(max_length=100, blank=False) and here is my

Customize the djoser create user endpoint

亡梦爱人 提交于 2020-02-01 19:01:05
问题 I am using djoser for auth purposes. I want to customize the create user end point of djoser. I have a User app. Here is my User model from django.db import models class User(models.Model): email = models.CharField(max_length=100, blank=False) name = models.CharField(max_length=100, blank=False) last_name = models.CharField(max_length=100, blank=False) account_address = models.CharField(max_length=30, blank=False) password = models.CharField(max_length=100, blank=False) and here is my

select multiple files in django with single filefield

巧了我就是萌 提交于 2020-02-01 09:05:28
问题 How can I select multiple files in a single fileField in django? I want to select different file formats in a single filefiled in django admin. **models.py** class FileModel(models.Model): name = models.CharField(max_length=100) file_upload = models.FileField(upload_to=settings=PATH) **admin.py** admin.site.register(FileModel,FileAdmin); In the admin I want to customize the file field to select multiple files to store in given path? Could you please help me? 回答1: You can't? The FileField was

Django QuerySet vs Raw Query performance

痞子三分冷 提交于 2020-02-01 07:59:47
问题 I have noticed a huge timing difference between using django connection.cursor vs using the model interface, even with small querysets. I have made the model interface as efficient as possible, with values_list so no objects are constructed and such. Below are the two functions tested, don't mind the spanish names. def t3(): q = "select id, numerosDisponibles FROM samibackend_eventoagendado LIMIT 1000" with connection.cursor() as c: c.execute(q) return list(c) def t4(): return list

Dropdown in Django Model

依然范特西╮ 提交于 2020-01-30 14:23:12
问题 I want to create a field in Django models.py which will render as a dropdown and user can select the options from there. If I have 5 choices: GREEN BLUE RED ORANGE BLACK How should I write my code in models.py and Forms.py so that the template renders it like a dropdown element? 回答1: Specify CharField or IntegerField with choices option in your model https://docs.djangoproject.com/en/1.8/ref/models/fields/#choices and use ModelForm https://docs.djangoproject.com/en/1.8/topics/forms/modelforms

Dropdown in Django Model

99封情书 提交于 2020-01-30 14:22:04
问题 I want to create a field in Django models.py which will render as a dropdown and user can select the options from there. If I have 5 choices: GREEN BLUE RED ORANGE BLACK How should I write my code in models.py and Forms.py so that the template renders it like a dropdown element? 回答1: Specify CharField or IntegerField with choices option in your model https://docs.djangoproject.com/en/1.8/ref/models/fields/#choices and use ModelForm https://docs.djangoproject.com/en/1.8/topics/forms/modelforms