Skip to content

Commit

Permalink
relation: trying autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
gythaogg committed May 13, 2024
1 parent 06cedab commit 88f259c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
34 changes: 31 additions & 3 deletions apis_core/relations/forms.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from django.forms import ModelForm
from django.forms import ModelForm, ModelChoiceField
from django.urls import reverse
from django.contrib.contenttypes.models import ContentType
from django_select2.forms import ModelSelect2Widget


from crispy_forms.layout import Submit, Layout, Div, HTML
from crispy_forms.helper import FormHelper

from apis_core.apis_metainfo.models import RootObject


class RelationForm(ModelForm):
def __init__(
Expand All @@ -26,15 +29,38 @@ def __init__(

if frominstance:
self.fields[subj].disabled = True
self.fields[subj].initial = frominstance
self.fields[subj].initial = RootObject.objects_inheritance.get_subclass(
pk=frominstance.pk
)
self.fields[subj].label = ContentType.objects.get_for_model(
frominstance
).name

if tocontenttype:
self.fields[obj].queryset = tocontenttype.model_class().objects.all()
# self.fields[obj].queryset = tocontenttype.model_class().objects.all()
# self.fields[obj].label = tocontenttype.name
if hasattr(tocontenttype.model_class(), "name"):
self.fields[obj] = ModelChoiceField(
queryset=tocontenttype.model_class().objects.all(),
widget=ModelSelect2Widget(
model=tocontenttype.model_class(),
search_fields=["name__icontains", "id__icontains"],
),
)
else:
self.fields[obj] = ModelChoiceField(
queryset=tocontenttype.model_class().objects.all(),
widget=ModelSelect2Widget(
model=tocontenttype.model_class(),
search_fields=["label__icontains", "id__icontains"],
),
)
self.fields[obj].label = tocontenttype.name

if kwargs.get("instance", None) is not None:
# allow subject also to be editable
#
pass
self.helper = FormHelper(self)

relcontenttype = ContentType.objects.get_for_model(self._meta.model)
Expand All @@ -45,8 +71,10 @@ def __init__(
if frominstance:
args.append(ContentType.objects.get_for_model(frominstance).pk)
args.append(frominstance.pk)

if tocontenttype:
args.append(tocontenttype.pk)

hx_post = reverse("apis:relation", args=args)
if inverted:
hx_post = reverse("apis:relationinverted", args=args)
Expand Down
12 changes: 12 additions & 0 deletions apis_core/relations/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from apis_core.apis_metainfo.models import RootObject
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.contrib.contenttypes.models import ContentType
from django.forms import modelform_factory
Expand Down Expand Up @@ -102,6 +103,17 @@ def get_object(self):

def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
relation = self.get_object()
print(
relation, RootObject.objects_inheritance.get_subclass(pk=relation.subj.pk)
)
kwargs["frominstance"] = RootObject.objects_inheritance.get_subclass(
pk=relation.subj.pk
)
kwargs["tocontenttype"] = ContentType.objects.get_for_model(
RootObject.objects_inheritance.get_subclass(pk=relation.obj.pk)
)

kwargs["embedded"] = False
return kwargs

Expand Down

0 comments on commit 88f259c

Please sign in to comment.