Render Wagtail InlinePanel on non-page model, without using snippet? - django-models

I have defined two models and linked them as seen below. The Order model should display OrderItems using a Wagtail InlinePanel. How can I get this to work without registering the Order model as a snippet?
from wagtail.admin.edit_handlers import InlinePanel
from wagtail.core.models import Orderable
from wagtail.snippets.models import register_snippet
from modelcluster.fields import (
ParentalKey,
)
from modelcluster.models import ClusterableModel
#register_snippet
class Order(ClusterableModel):
panels = [
InlinePanel("items", label="Order items"),
]
def __str__(self):
return f"Order {self.id}"
def get_total_cost(self):
return sum(item.get_cost() for item in self.items.all())
class OrderItem(Orderable):
order = ParentalKey(
Order,
related_name="items",
on_delete=models.CASCADE,
blank=False,
)
product = models.CharField(max_length=255)
price = models.DecimalField(max_digits=10, decimal_places=2)
quantity = models.PositiveIntegerField(default=1)
panels = [
FieldPanel("product"),
FieldPanel("price"),
FieldPanel("quantity"),
]

Create wagtail_hooks.py in the same app where models.py is found. Then, in wagtail_hooks.py, do something like this (plenty of extras put in the OrderAdmin class here for you to check out - note that not all of the fields will match yours):
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
class OrderAdmin(ModelAdmin):
model = Order
menu_order = -100
menu_label = 'Orders'
menu_icon = 'fa-shopping-cart'
list_display = ('number', 'customer', 'date_placed', 'total', 'payment_method', 'status')
list_filter = (OrderStatusFilter, )
search_fields = ('number', 'customer')
inspect_view_enabled = True
inspect_view_fields = ['number', 'status', 'customer', 'shipping_address', 'total', 'subtotal', 'shipping', 'handling', 'tax', 'date_placed', 'notes', 'payment_method', 'payment_card']
modeladmin_register(OrderAdmin)
The above will let you view the Order and it's associated OrderItems without having to declare Order as a snippet. Reference

Related

AbstractBaseUser.get_username() missing 1 required positional argument: 'self' | Error while accessing current user's username

I am trying to access the Id of current logged in User. but i am getting the below error.
models.py of derived model
from django.db import models
from django.urls import reverse, reverse_lazy
from django.utils import timezone
from accounts.models import CustomUser
# Create your models here.
class PostProblem(models.Model):
problem_type_choices = (
('c','Confidential'),
('sc','Semi-confidential'),
('p','Public')
)
problem_category_choices = (
('agriculture','Agriculture'),
('computer_science','Computer Science'),
('social_studies','Social Studies'),
('environment','Environmental Science'),
('mathematics','Mathematics'),
('engineering','Engineering'),
('physics','physics'),
('chemistry','chemistry'),
('other','Other')
)
author = models.ForeignKey("accounts.CustomUser", verbose_name= "Creater", default = CustomUser.get_username ,on_delete=models.CASCADE)
problem_title = models.CharField(max_length=200, verbose_name="Problem's title")
problem_type = models.CharField(choices=problem_type_choices,max_length=5, verbose_name='Confidentiality of the problem ')
problem_category = models.CharField(choices=problem_category_choices, max_length=50, verbose_name="Catrgory of the problem")
problem_brief = models.CharField(max_length=1000, verbose_name='Breif description of the problem ')
problem_description = models.TextField(verbose_name='Problem complete description ')
problem_reward = models.IntegerField(verbose_name='Prize money for providing the solution ')
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
def publish(self):
self.published_date = timezone.now
self.save()
def __str__(self):
return self.problem_title
def get_absolute_url(self):
return reverse("problem_detail", kwargs={"pk": self.pk})
def approve_solutions(self):
return self.solutions.filter(approved_solutions = True)
views.py
from django.shortcuts import render
from django.urls import reverse_lazy
from django.utils import timezone
from django.contrib.auth.mixins import LoginRequiredMixin
from problems.models import PostProblem, Solutions
from problems.forms import PostProblemForm, SolutionsForm
from django.views.generic import TemplateView, CreateView, DetailView, DeleteView, UpdateView, ListView
# Create your views here.
class PostProblemCreateView(CreateView, LoginRequiredMixin):
login_url = 'login/'
redirect_field_name = 'problems/problem_detail.html'
form_class = PostProblemForm
model = PostProblem
forms.py
from django import forms
from problems.models import PostProblem, Solutions
class PostProblemForm(forms.ModelForm):
class Meta:
model = PostProblem
fields = ("problem_title","problem_type","problem_category","problem_brief","problem_description","problem_reward")
widgets = {
'problem_title':forms.TextInput(attrs={'class':'textinputclass'}),
'problem_type': forms.TextInput(attrs={'class':'choice_input'}),
'problem_category':forms.TextInput(attrs={'class':'choice_input'}),
'problem_brief': forms.Textarea(attrs={'class':'editable medium-editor-textarea post_brief'}),
'problem_description': forms.Textarea(attrs={'class':'editable medium-editor-textarea post_complete'}),
'problem_reward': forms.TextInput(attrs={'class':'textinputclass'})
}
model.py of base model
from django.db import models
from django.contrib import auth
from django.urls import reverse
# Create your models here.
# for custom user
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, User
from .managers import CustomUserManager
class CustomUser(AbstractBaseUser, PermissionsMixin):
'''Model representation for user'''
user_type_choices = (
('ps','Problem Solver'),
('pp','Problem Provider')
)
account_type_choices = (
('o','Organization'),
('i','Individual')
)
user_type = models.CharField(max_length=5, choices=user_type_choices, default='pp', verbose_name="Who you are? ")
account_type = models.CharField(max_length=5, choices= account_type_choices, default='o', verbose_name="Account Type ")
email = models.EmailField(max_length=50, unique=True, blank=False, verbose_name="Your Email ")
is_active = models.BooleanField(default=True) # anyone who signs up for thsi application is by default an active user
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False) # the person who has highest level of control over database
# need to specify manager class for this user
objects = CustomUserManager()
# we are not placing password field here because the password field will always be required
REQUIRED_FIELDS = ['user_type', 'account_type']
USERNAME_FIELD = 'email'
EMAIL_FIELD = 'email'
I searched the web for answers but they have mentioned only about accessing current user id in function based views. How can I resolve this kind of error? I am new to Django.

How to make required filling in a form field with a many-to-many relationship in django admin

I have a Recipes model with an ingredients field with a many-to-many relationship. I have an intermediate Ingredient amount model associated with the ingredients field via through. I want to prohibit the creation of a recipe in django admin without ingredients. I'm trying to solve this problem by creating my own class inherited from BaseInlineFormSet and redefining the clean() method. It doesn't work out yet, I need help, what am I doing wrong?
admin.py
class IngredientsAmountInlineFormset(BaseInlineFormSet):
def clean_ingredients(self):
if len(self.cleaned_data['ingredients']) < 1:
return 'Укажите хотя бы один ингредиент в рецепте'
return self.cleaned_data['ingredients']
class IngredientsAmountInline(admin.TabularInline):
model = IngredientsAmount
formset = IngredientsAmountInlineFormset
extra = 0
#admin.register(Recipes)
class RecipesAdmin(admin.ModelAdmin):
list_display = ('name', 'author', 'favorites_count',)
list_filter = ('name', 'author', 'tags',)
search_fields = ('name', 'author', 'tags',)
empty_value_display = '-пусто-'
inlines = (IngredientsAmountInline,)
models.py
class Ingredients(models.Model):
name = models.CharField(max_length=200, unique=True)
measurement_unit = models.CharField(max_length=200)
...
class Recipes(models.Model):
...
ingredients = models.ManyToManyField(Ingredients,
through='IngredientsAmount',
related_name='recipes',
verbose_name='Ингредиенты',
)
...
class IngredientsAmount(models.Model):
ingredients = models.ForeignKey(Ingredients,
on_delete=models.CASCADE,
related_name='amount',
verbose_name='Ингредиенты',
)
recipes = models.ForeignKey(Recipes,
on_delete=models.CASCADE,
related_name='amount',
verbose_name='Рецепты', )
amount = models.IntegerField()
obiviously clean should raise ValidationException:
class IngredientsAmountInlineFormset(BaseInlineFormSet):
def clean_ingredients(self):
value = self.cleaned_data['ingredients']
if value:
return value
raise ValidationError(_('Укажите хотя бы один ингредиент в рецепте'))
by the way:
Try don't use the direct text message:
'Укажите хотя бы один ингредиент в рецепте'
Wrap your text in gettext/gettext_lazy, like this:
from django.utils.translation import gettext_lazy as _
_('Укажите хотя бы один ингредиент в рецепте')
It help you in future collect those messages together, and translate it if you need.

How check UniqueConstraint in Django with CreateView and custom forms if field set in view?

I do:
I define UniqueConstraint (also try with 'unique_together') in model:
class Project(models.Model):
class Meta:
constraints = [
models.UniqueConstraint(
fields=['company', 'name'], name="unique_project_name_in_company"
)
]
name = models.CharField(blank=False, max_length=256)
company = models.ForeignKey(
Company,
on_delete=models.CASCADE
)
I set company in form_valid in view (I think it's reason of my problem):
class ProjectCreateView(LoginRequiredMixin, generic.CreateView):
model = Project
form_class = ProjectForm
def form_valid(self, form):
form.instance.company = self.request.user.company
return super().form_valid(form)
I try define message for 'unique_project_name_in_company' in form:
class ProjectForm(forms.ModelForm):
model = Project
class Meta:
model = Project
fields = ['name']
error_messages = {
NON_FIELD_ERRORS: {
'unique_project_name_in_company': "Name isn't unique!",
}
}
Unexpected behavior
If I submit form with non-unique pair (inputed non-unique name) I want get my custom error_message but I get:
500 IntegrityError UNIQUE constraint failed: company_id, name

Django migration doesn't contain composite foreign key relationship and composite primarykey with MSSQL database

I just start new Django project with existing MSSQL (not mysql) database. In database I have four table which all are in relationship with together. I build the model for existing table bypython manage.py inspectdb (table _name). but it doesn’t gives any relation neither foreign key nor OneToOne. So I update the models.py as per table relationship.
Note that each table has composite primary key and composite foreign key. I define composite primary key with unique_togather=() and for composite foreign key by using Django third party library
Django-composite-foreignkey module.
But when I tried to migrate .it doesn’t seem to establish the relationship
models.py
from django.db import models
from compositefk.fields import CompositeForeignKey, CompositeOneToOneField
class Company(models.Model):
code = models.DecimalField(db_column='Code', max_digits=38, decimal_places=0)
srccode = models.SmallIntegerField(db_column='SrcCode')
est = models.DateTimeField(db_column='Est')
rownum = models.BigIntegerField(db_column='RowNum')
class Meta:
manage = False
unique_together = ('code', 'srccode')
db_table = 'Company'
class Department(models.Model):
depcode = models.DecimalField(db_column='DepCode', max_digits=38, decimal_places=0)
depsrccode = models.SmallIntegerField(db_column='Depsrccode')
name = models.CharField(db_column='Name')
rownum = models.BigIntegerField(db_column='RowNum')
class Meta:
manage = False
unique_together = ('depcode', 'depsrccode')
db_table = 'department'
class Floor(models.Model):
code = models.DecimalField(db_column='Code', max_digits=38, decimal_places=0)
srccode = models.SmallIntegerField(db_column='SrcCode')
depcode = models.DecimalField(db_column='DepCode', max_digits=38, decimal_places=0)
depsrccode = models.SmallIntegerField(db_column='Depsrccode')
floorname = models.CharField(db_column='FloorName')
rownum = models.BigIntegerField(db_column='RowNum')
company = CompositeForeignKey(Company,on_delete=models.CASCADE,to_fields={'code':'code','srccode': 'srccode'})
department= CompositeOneToOneField(Department,on_delete=models.CASCADE,to_fields={'depcode':'depcode','depsrccode': 'depsrccode'})
class Meta:
manage = False
unique_together = ('depcode', 'depsrccode','floorname')
db_table = 'floor'
class SubCompany(models.Model):
code = models.DecimalField(db_column='Code', max_digits=38, decimal_places=0)
srccode = models.SmallIntegerField(db_column='SrcCode')
subname = models.CharField(db_column='SubName')
rownum = models.BigIntegerField(db_column='RowNum')
company = CompositeForeignKey(Company,on_delete=models.CASCADE,to_fields={'code':'code','srccode': 'srccode'})
class Meta:
manage = False
unique_together = ('code', 'srccode','subname','rownum')
db_table = 'SubCompany'
I add all composite foreign and primary key constrain after auto create models from inspectdb.
My initial migration file as per below
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='SubCompany',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('code' = models.DecimalField(db_column='Code', max_digits=38, decimal_places=0)),
('srccode' = models.SmallIntegerField(db_column='SrcCode')),
('subname' = models.CharField(db_column='SubName')),
('rownum' = models.BigIntegerField(db_column='RowNum')),
],
options={
'db_table': 'SubCompany',
'managed': False,
},
),
migrations.CreateModel(
name='Company',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('code' = models.DecimalField(db_column='Code', max_digits=38, decimal_places=0)),
('srccode' = models.SmallIntegerField(db_column='SrcCode')),
('est' = models.DateTimeField(db_column='est')),
('rownum' = models.BigIntegerField(db_column='RowNum')),
],
options={
'db_table': 'Company',
'managed': False,
},
),
migrations.CreateModel(
name='Floor',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('code' = models.DecimalField(db_column='Code', max_digits=38, decimal_places=0)),
('srccode' = models.SmallIntegerField(db_column='SrcCode')),
('depcode' = models.DecimalField(db_column='DepCode', max_digits=38, decimal_places=0)),
('depsrccode' = models.SmallIntegerField(db_column='DepSrcCode')),
('name' = models.CharField(db_column='Name')),
('rownum' = models.BigIntegerField(db_column='RowNum')),
],
options={
'db_table': 'Floor',
'managed': False,
},
),
migrations.CreateModel(
name='Department',
fields=[
('depcode' = models.DecimalField(db_column='DepCode', max_digits=38, decimal_places=0)),
('depsrccode' = models.SmallIntegerField(db_column='DepSrcCode')),
('name' = models.CharField(db_column='Name')),
('rownum' = models.BigIntegerField(db_column='RowNum')),
],
options={
'db_table': 'Department',
'managed': False,
},
),
]
Why in migration file doesn’t have all relationship and primary key constrain.? Is there anything that I am missing.? how can i build the models as per my table relations? Any help much appreciate.
requirements.txt
Django==2.1.7
django-composite-foreignkey==1.1.0
django-crispy-forms==1.7.2
django-extensions==2.1.6
django-filter==2.1.0
django-mssql==1.8
django-pyodbc-azure==2.1.0.0
djangorestframework==3.9.2
graphviz==0.10.1
Markdown==3.0.1
pydotplus==2.0.2
pyodbc==4.0.26
pyparsing==2.3.1
pytz==2018.9
pywin32==224
six==1.12.0
The short answer is that Django does not support composite primary keys/Index combos like entity framework does. Since it's not natively supported and it appears to be a business requirement, I might suggest using another ORM framework. If you're bound to Django, you can read more here:
https://code.djangoproject.com/wiki/MultipleColumnPrimaryKeys
You have to write like this tuple inside tuple because of multiple row will be composite pk.
class Meta:
manage = False
unique_together = (('depcode', 'depsrccode'),)
db_table = 'department'

Nested categories/InlinePanel(s) in wagtail

I struggle to implement something like "nested categories":
PageA:
- Cat1
- SubCat1
- SubCat2
- ...
- Cat2
- SubCat1
- ...
All categories and subcategories should be orderable and editable by an editor.
My guess was something like this:
class CategoryTestPage(Page):
content_panels = Page.content_panels + [
InlinePanel('categories')
]
class Category(Orderable,ClusterableModel,models.Model):
page = ParentalKey(CategoryTestPage, related_name='category')
category = models.CharField(max_length=250)
def __str__(self):
return "%d %s" % (self.id, self.category)
panels = [
FieldPanel('category'),
InlinePanel('subcategory')
]
class SubCategory(Orderable,models.Model):
category = ParentalKey(ProjektOrdnung, related_name='subcategory')
subcategory = models.CharField(max_length=250)
def __str__(self):
return "%d %s" % (self.id, self.subcategory)
panels = [
FieldPanel('subcategory')
]
But this results in 'CategoryForm' object has no attribute 'formsets'. It seems nested InlinePanels are the problem?
Further I need this "hierarchical taxonomy" for assigning some of these categories/subcategories to other pages:
PageB:
- has Cat1
- has SubCa2
- ...
... which looks a lot like hierarchical tags...
Any ideas how to implement this or what's wrong with my implementation?
Kind regards,
tombreit
PS: I'm on wagtail 1.2rc1
Here's one way to do it, with much room for interface improvements ;) In order to sort the categories at the page level, I'd suggest the use of django-sortedm2m.
from wagtail.wagtailcore.models import Orderable, Page
from wagtail.wagtailsnippets.models import register_snippet
from django.db import models
#register_snippet
class Category(models.Model):
name = models.CharField(
max_length=80, unique=True, verbose_name=_('Category Name'))
slug = models.SlugField(unique=True, max_length=80)
parent = models.ForeignKey(
'self', blank=True, null=True, related_name="children",
help_text=_(
'Categories, unlike tags, can have a hierarchy. You might have a '
'Jazz category, and under that have children categories for Bebop'
' and Big Band. Totally optional.')
)
description = models.CharField(max_length=500, blank=True)
class Meta:
ordering = ['name']
verbose_name = _("Category")
verbose_name_plural = _("Categories")
panels = [
FieldPanel('name'),
FieldPanel('parent'),
FieldPanel('description'),
]
def __str__(self):
return self.name
def clean(self):
if self.parent:
parent = self.parent
if self.parent == self:
raise ValidationError('Parent category cannot be self.')
if parent.parent and parent.parent == self:
raise ValidationError('Cannot have circular Parents.')
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.name)
return super(Category, self).save(*args, **kwargs)
class CategoryPage(models.Model):
category = ParentalKey('Category', related_name="+", verbose_name=_('Category'))
page = ParentalKey('MyPage', related_name='+')
panels = [
FieldPanel('category'),
]
class MyPage(Page):
categories = models.ManyToManyField(Category, through=CategoryPage, blank=True)
content_panels = Page.content_panels + [
FieldPanel('categories'),
]

Resources