I've struggled for weeks with Django's ModelFormSet & co, head exploding.
I searched hours,days on the internet to find a solution, but nothing really helpful for my special problem:
I have 2 models (here simplified):
class BaseSetting(models.Model):
"""This is a basic model which holds namespace and key of a setting."""
namespace = models.CharField(max_length=25)
key = models.CharField(max_length=255)
class ScopedSetting(models.Model):
"""Model class for all scoped MedUX settings.
Settings are generally saved as strings, but are interpreted at retrieval
and casted into their correct types. ScopedSettings knows about
``str``, ``int``, ``bool``"""
base = models.ForeignKey(BaseSetting, on_delete=models.CASCADE)
"""The FK to the basic settings fields like namespace, key."""
tenant = models.ForeignKey(
Tenant,
verbose_name=_("Tenant"),
on_delete=models.CASCADE,
default=None,
null=True,
blank=True,
)
group = models.ForeignKey(
Group, on_delete=models.CASCADE, default=None, null=True, blank=True
)
device = models.ForeignKey(
Device, on_delete=models.CASCADE, default=None, null=True, blank=True
)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
default=None,
null=True,
blank=True,
)
scope = models.IntegerField(choices=Scope.choices)
"""The scope where this setting is valid."""
value = models.CharField(max_length=255, null=True)
ATM I am using django-extra-views to get a simple ModelFormSetView - and it shows a form, but not what I want:
For each BaseSetting (namespace,key) there exists up to 5 different ScopedSettings which could be applicable to the current user, depending on their scope: the vendor, the current tenant, group, device, or user: So each user can have an own setting, each tenant etc., but the user setting for one user, if available as DB object, overrides the vendor's setting. There is a clear permissions system and scope cascade to check which setting is applicable, and all this works. I just want to edit those settings.
So I'd like to have a View under /settings/<base_setting_pk>/ which shows basically (simplified, its even more complicated) all 5 scopes: VENDOR, TENANT, GROUP, DEVICE, USER.
This is the perfect use case for a formset IMHO, BUT: It should show all these 5 Scopedsettings, even when they are not present in the DB.
Here extra=... comes in, and I am able to show the correct number of items.
class BaseSettingsDetailView(HtmxMixin, ModelFormSetView):
# ScopedPermissionRequiredMixin,
model = ScopedSettings
form_class = ScopedSettingsForm
formset = ScopedSettingsModelFormSet
fields = ["value", "scope"]
enforce_htmx = False
ordering = ["scope"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def setup(self, request, *args, **kwargs):
super().setup(request, *args, **kwargs)
self.base_instance = self.get_queryset().first().base
self.scopes = SettingsRegistry.scopes(
self.base_instance.namespace, self.base_instance.key
)
self.additional = []
def get_queryset(self):
return super().get_queryset().filter(base_id=self.kwargs.get("pk"))
def get_factory_kwargs(self):
def formfield_callback(field_name) -> forms.Field:
print(field_name)
if field_name:
return forms.CharField()
for scope in self.scopes:
if self.get_queryset().filter(scope=scope.value).exists():
pass
else:
self.additional.append(
ScopedSettings(base=self.base_instance, scope=scope)
)
factory_kwargs = super().get_factory_kwargs()
max_num = len(self.get_queryset()) + len(self.additional)
factory_kwargs.update(
{"max_num": max_num, "formfield_callback": formfield_callback}
)
return factory_kwargs
But I cant get the Form to show correct data in the empty, uninitialized, extra fields:
They are just labelled "value".
What I need is 5 fields, each labelled by scope. Some of them should be populated with the queryset (this could be the first and the third e.g.), and the others should be empty, but labelled coorectly (and prefilled with the missing scope, each one)
I hope this is understandable.
Any hint welcome.
Related
I am trying to override the save method in a model with logic to update a couple of many to many fields. Using print statements I can see values updating as expected but the values are not persisted after save.
In the below model the change_access_flag is changing as expected with a signal, the prints are executing with the appropriate values, but the allowed_departments and allowed_communities fields are not updating with the printed values.
Model
class Person(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
full_name = models.CharField(null=True, blank=True, max_length=50)
payroll_id = models.CharField(null=True, max_length=20)
position = models.ForeignKey(Position, null=True, on_delete=models.SET_NULL)
primary_community = models.ForeignKey(Community, null=True, on_delete=models.CASCADE, related_name="primary_community")
region = models.CharField(max_length=2, choices=RegionChoices.choices, blank=True, null=True)
allowed_communities = models.ManyToManyField(Community, blank=True, related_name="allowed_community")
allowed_departments = models.ManyToManyField(Department, blank=True)
access_change_flag = models.BooleanField(default=False)
def __str__(self):
return f'{self.user.first_name} {self.user.last_name}'
class Meta:
verbose_name_plural = "People"
ordering = ['position__position_code', 'user__last_name', 'full_name']
def save(self, *args, **kwargs):
#Set Full Name field
if self.user.last_name:
self.full_name = f'{self.user.first_name} {self.user.last_name}'
super().save(*args, **kwargs)
#Change flag set in signals, set for events that require updating access settings
if self.access_change_flag:
self.access_change_flag = False
#Allowed community access
access_level = self.position.location_access_level
self.allowed_communities.clear()
if access_level == 'R':
if self.primary_community.community_name == '#':
region = self.region
else:
region = self.primary_community.region
if region is not None:
communities = Community.objects.filter(region=region)
self.allowed_communities.set(communities)
self.allowed_communities.add(self.primary_community)
elif access_level == 'A':
communities = Community.objects.filter(active=True)
self.allowed_communities.set(communities)
else:
communities = self.primary_community
self.allowed_communities.add(communities)
print(self.allowed_communities.all())
#Allowed department access
dept_access = self.position.department_only_access
if dept_access:
depts = [self.position.department]
else:
depts = Department.objects.filter(active=True)
self.allowed_departments.set(depts)
print(self.allowed_departments.all())
super().save(*args, **kwargs)
I have tried variations of set, clear, add, moving the super.save() around, and placing the logic in a signal but nothing seems to work. I have tested initiating save from both a model form through a view and admin.
Let me answer in quotes. You can find the source in this section.
If you wish to update a field value in the save() method, you may also
want to have this field added to the update_fields keyword argument.
This will ensure the field is saved when update_fields is specified.
Also read here
Specifying update_fields will force an update.
So try to call the super().save(*args, **kwargs) method at the end with defining the argument update_fields. This will force the update of your model regarding the specified fields.
Let me know how it goes.
I have a page that displays a list of interviewers and some important info about each user.
One of the things that I want it to show is the number of users who have been interviewed by that specific interviewer.
I wrote a view like this:
class ManagerUsers(ListView):
model = User
template_name = 'reg/manager-users.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['scientific_interviewers'] = User.objects.filter(role='theory_interviewer').all()
context['interviewed_number'] = len(ScientificInfo.objects.filter(user__role='applicant', is_approved=True, interviewer=?????))
the interviewer field should be equal to that object's user but I don't know what to do exactly.
the output should be something like this:
object 1 : user's name, user's other info, user's interviewed_number
....
these are my models:
USER_ROLE_CHOICES = (('0', 'applicant'),
('1', 'theory_interviewer'),)
class User(AbstractUser):
id = models.AutoField(primary_key=True)
role = models.CharField(max_length=25, null=True, choices=USER_ROLE_CHOICES, default=USER_ROLE_CHOICES[0][0])
username = models.CharField(unique=True, max_length=13)
first_name = models.CharField(max_length=32, null=True, default=None)
last_name = models.CharField(max_length=64, null=True, default=None)
class ScientificInfo(models.Model):
id = models.AutoField(primary_key=True)
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='user')
interviewer = models.OneToOneField(User, on_delete=models.CASCADE, related_name='interviewer')
is_approved = boolean field
You can override the .get_queryset() method [Django-doc] to return only the interviewers. By using .annotate(…) [Django-doc] you can add an extra attribute to these Users:
from django.db.models import Count
class ManagerUsersView(ListView):
model = User
context_object_name = 'scientific_interviewers'
template_name = 'reg/manager-users.html'
def get_queryset(self):
return super().get_querset().filter(
role='1'
).annotate(
interviewed_number=Count('interviewer', filter=Q(interviewer__user__role='0', interviewer__is_approved=True))
)
The Users that arise from this queryset will have an extra attribute .interviewed_number with the number of approved ScientificInfos where that user was the interviewer.
Note: In Django, class-based views (CBV) often have a …View suffix, to avoid a clash with the model names.
Therefore you might consider renaming the view class to ManagerUsersView, instead of ManagerUsers.
Note: The related_name=… parameter [Django-doc]
is the name of the relation in reverse, so from the User model to the ScientificInfo
model in this case. Therefore it (often) makes not much sense to name it the
same as the forward relation. You thus might want to consider renaming the interviewer relation to interviews.
I'm writing and app to manage my network equipments. I created a model, RJ45port, which I can add to my equipment as needed. A RJ45port can be plugged into an other RJ45port and only one.
Here is the model I created :
class RJ45port(models.Model):
plugged_into = models.OneToOneField('self', on_delete=models.SET_NULL, blank=True, null=True)
When I "plug" a RJ45port into another, I want the second one to have "plugged_into" set to the first one. I want the relation to be symmetrical. If I "unplug", I want both of the RJ45 ports to have "plugged_into" set to null, or blank.
I found a bit of code, it might be a hint :
def save(self, *args, **kwargs):
super(RJ45port, self).save()
self.plugged_into.plugged_into = self
To be honest I'm a bit lost here and it's the final step I need to get this app functional...
You are best suited just making a model plug_into() method, and then using it to "plug" one instance into another, as well as an unplug() method.
Example:
class RJ45port(models.Model):
plugged_into = models.OneToOneField('self', on_delete=models.SET_NULL, blank=True, null=True)
def plug_into(self, instance):
self.plugged_into = instance
instance.plugged_into = self
self.save(update_fields=['plugged_into'])
instance.save(update_fields=['plugged_into'])
return [self.plugged_into, instance.plugged_into]
def unplug(self):
self.plugged_into.plugged_into = None
self.plugged_into = None
self.plugged_into.save(update_fields=['plugged_into'])
self.save(update_fields=['plugged_into'])
return [self.plugged_into, instance.plugged_into]
And then you can call it like this:
port_1 = Port.objects.all()[0] # First port
port_2 = Port.objects.all()[1] # Second port
port_1.plug_into(port_2) # Should return [instance, instance]
port_1.unplug() # Should return [None, None]
You are right. Simply override the save method. But call super().save() at the end:
class RJ45port(models.Model):
plugged_into = models.OneToOneField('self', on_delete=models.SET_NULL, blank=True, null=True)
def save(self, *args, **kwargs):
self.plugged_into.plugged_into = self
super(RJ45port, self).save()
Another option is to use a related_name so you can make a reverse access from the referenced instance, so you can say that the relationship becomes "symetrical". The only downside is that you can't use the same name to reference both connections:
class RJ45port(models.Model):
plugged_into = models.OneToOneField('self', on_delete=models.SET_NULL, blank=True, null=True, related_name='plugged_from')
In this example, plugged_from can be queried like any other field from the referenced instance.
core/models.py
from django.db import models
from django.db.models.signals import post_save
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
middle_name = models.CharField(max_length=30)
class Company(models.Model):
name = models.CharField(max_length=100)
class Entity(models.Model):
is_person = models.BooleanField(default=True)
person = models.ForeignKey(Person, on_delete=models.PROTECT, null=True)
company = models.ForeignKey(Company, on_delete=models.PROTECT, null=True)
name = models.CharField(max_length=30)
def __str__(self):
return self.name
#property
def title(self):
return self.name
class Meta:
verbose_name_plural = 'entities'
def post_save_person_receiver(sender, instance, created, *args, **kwargs):
if created:
entity, is_created = Entity.objects.get_or_create(is_person=True, person=instance, company=None, name=instance.last_name) # noqa
post_save.connect(post_save_person_receiver, sender=Person)
def post_save_company_receiver(sender, instance, created, *args, **kwargs):
if created:
entity, is_created = Entity.objects.get_or_create(is_person=False, person=None, company=instance, name=instance.short_name) # noqa
post_save.connect(post_save_company_receiver, sender=Company)
class Group(models.Model):
name = models.CharField(max_length=20)
is_individual = models.BooleanField(default=True)
members = models.ManyToManyField(Entity, through='Membership')
class Membership(models.Model):
group = models.ForeignKey(Group, on_delete=models.PROTECT, null=False)
entity = models.ForeignKey(Entity, on_delete=models.PROTECT, null=False)
class Meta:
unique_together = ("entity", "group")
For every Company and Person created, an Entity is automatically created where Entity.is_person=True if it's a Person. An Entity can then become a member of a Group such as 'Employee', 'Supplier' and 'Customer' through a ManyToMany relationship in the Membership Model.
How do I filter Membership.entity in Admin View (for add and update) that when the Group selected is an 'is_individual=True', such as 'Employee', Entity Field only shows 'is_person=True' Persons in the Entity combobox?
Admin View
I would consider writing your own view for this. In my opinion, Djangos admin is not a good site to base a frontend on. I use it only for making quick changes where validation doesn't need to be done as much because the users of admin should know what they're doing. When you start adding more responsive stuff, that's when I consider using another view and starting from scratch (or a framework) but a different view none-the-less.
There are several different names for what it sounds like you want: dependent dropdowns, chained fields, etc. Django itself doesn't have anything out of the box for this.
Your 2 options are: (1) do it yourself or (2) use a 3rd-party package.
As far as doing it yourself goes, you're going to need to do some work in JS for making it work on the frontend, and you're probably going to need some kind of flexible view that outputs JSON data, and you're going to need some kind of custom select field to handle this.
For using a 3rd-party package, I don't see anything particularly recent that does this. Here's one that's a few years old: https://github.com/runekaagaard/django-admin-flexselect
Hope this helps some!
I'm using Django 1.5 and am struggling with something that I guess is pretty basic.
I've got following Persons model:
class Person(models.Model):
contact_person = models.ManyToManyField(ContactPerson)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
...
The goal is to add a partner of the person to the model. I don't want the partner or the person to be superior to the other; they should be equal.
In other words, when looking up a person, his/her partner should appear as well (if there is one). I need to be able to add the partner once, either at the woman's or at the men's side (I don't want to link them two times).
I've searched the documentation for OneToOneFields, but recursive OneToOnes don't seem to be supported, i.e. I get a NameError ("name 'Person' is not defined") when I try:
partner = models.OneToOneField(Person, blank=true, null=true)
Can anyone point me in the right direction?
Try this:
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
...
partner = models.OneToOneField('self', null=True, blank=True)
def save(self, checkPartner = True, *args, **kwargs):
super(Person, self).save()
if self.partner and checkPartner:
self.partner.partner = self
self.partner.save(checkPartner = False)
Null true on partner field, has to be enabled because the first person to be saved will not have any partner.
checkPartner parameter has been added so that save() doesn't fall into an infinite loop
https://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey
partner = models.OneToOneField('Person', blank=true, null=true)
or in alternative, if you're on the same class
partner = models.OneToOneField('self', blank=true, null=true)
quotes are everything, in this case
def save(self, *args, **kwargs):
# call to super, we want self.partner to be set
super(Person, self).save(*args, **kwargs)
# this is necessary to avoid infinite save loops on partner's save call
# at this point, you have a partner
# this won't work if your partner has already a partner
# but it's easy to go from here
if not self.partner.partner:
self.partner.partner = self
self.partner.save()