django-models

Inheriting from both ABC and django.db.models.Model raises metaclass exception

三世轮回 提交于 2020-06-16 17:15:07
问题 I am trying to implement a Django data model class, which is also an interface class, using Python 3. My reason for doing so is, I'm writing a base class for my colleague, and need him to implement three methods in all of the classes he derives from mine. I am trying to give him a simplified way to use the functionality of a system I've designed. But, he must override a few methods to supply the system with enough information to execute the code in his inherited classes. I know this is wrong,

Adding extended Profile model into custom user models admin

倖福魔咒の 提交于 2020-06-16 04:55:50
问题 How can i add extended Profile model fields (fields which are not available in custom user model fields) into custom users admin users.admin ? what i am trying to do is that i want too see Profile model fields like photo, date_of_birth, country, phone etc.. inside the Personal Info(see in image) & i can make changes in it from here. profile model from django.db import models from django.dispatch import receiver from django.urls import reverse from django.db.models.signals import post_save

Django __str__ returned non-string (type NoneType)

筅森魡賤 提交于 2020-06-16 04:50:11
问题 I am getting __str__ returned non-string (type NoneType) error at edit Product Model object Product Model class Product(models.Model): ProductName = models.CharField(blank=True, max_length=250) Price = models.FloatField(blank=True, default=9.99) Tax = models.FloatField(blank=True, null=True, default=0.0) StoreId = models.IntegerField(blank=True, null=True) RelatedStore = models.ForeignKey(Store, blank=True, null=True) Category = models.CharField(max_length=200, blank=True, null=True, default=

Reformat Django REST Framework Serializer to get Output

只谈情不闲聊 提交于 2020-06-16 03:35:10
问题 I am trying to get data in a particular format but i'm not able to get the desired output. My Model: class Category(models.Model): name = models.CharField(max_length=40) class Expense(models.Model): category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="category_name") description = models.CharField(max_length=200) total_amount = models.IntegerField() class Expense_Details(models.Model): expense = models.ForeignKey(Expense, on_delete=models.CASCADE, related_name=

django query all with filtering on the related set?

怎甘沉沦 提交于 2020-06-14 07:18:23
问题 class Customer(models.Model): name = models.CharField(max_length=200) # .. class CustomerTicket(models.Model): customer = models.OneToOneField(Customer) date = models.DateTimeField(auto_now_add=True) # .. I want to query all customers. And, adding for each customer its ticket if it has one in the date range - so I will get the ticket object only if it is in the given date range, otherwise the ticket field would be null. 回答1: Try this: from django.db import models customers = Customer.objects

django query all with filtering on the related set?

Deadly 提交于 2020-06-14 07:16:28
问题 class Customer(models.Model): name = models.CharField(max_length=200) # .. class CustomerTicket(models.Model): customer = models.OneToOneField(Customer) date = models.DateTimeField(auto_now_add=True) # .. I want to query all customers. And, adding for each customer its ticket if it has one in the date range - so I will get the ticket object only if it is in the given date range, otherwise the ticket field would be null. 回答1: Try this: from django.db import models customers = Customer.objects

Django Model: drop a field

我是研究僧i 提交于 2020-06-12 08:29:06
问题 Is there a straightforward clean way to drop a field from Django model? for addition of a field, migrate works perfect but its not working for deletion throwing the following error django.db.utils.DatabaseError: (1060, "Duplicate column name 'xyz_json_old'") 回答1: If you want to drop only the field from the model try this $ python manage.py dbshell You will get directly within your database shell (mysql or psql) it up to what database you are using. $ mysql> | psql> ALTER TABLE <table_name>

Are Django Model instances Hashable?

て烟熏妆下的殇ゞ 提交于 2020-06-11 16:40:07
问题 Are Django Model instances Hashable? For example, can I use a Django Model instance as a dictionary key, or create a Set of unique models? If they are Hashable, what causes two Django Model instances to be considered the same? Does it implement Hashable naively such that it only consider them to be the same if they are the same Python object in memory, or does it use the value of the Model instance in some way? 回答1: Model instances are Hashable. They are considered to be the same if they are

Django csrf token for Ajax

北城余情 提交于 2020-06-11 06:09:30
问题 I have given {% csrf_token %} inside the form. Do I have to give another {% csrf_token %} inside the AJAX $.ajax({ .......... )} ? <form method="post" data-validate-username-url="{% url 'validate_username' %}"> {% csrf_token %} {{ form.as_p }} <button type="submit">Sign up</button> </form> <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script> <script> $("#id_username").change(function () { console.log($(this).val()); var form = $(this).closest("form"); $.ajax({ url: form.attr(

how to create serializer for an enum field in django rest framework

余生长醉 提交于 2020-06-10 10:10:49
问题 i am writing an API in python django rest framework and i am stuck at creating a serializer field for an ENUM, how can i create a serializer field for an ENUM field. my model code is: class Queue(models.Model): class Meta: db_table = 'queues' id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True) name = models.CharField(max_length=45) type = EnumChoiceField(QueueTypes, default=QueueTypes.pending) date = models.DateTimeField(auto_now=True) and i am writing a