Re: How to replace content_type and object_id fields by a field with actual object in admin inline?

2016-10-14 Thread Anton Ponomarenko


Found solution.


Add form to admin.TabularInline:

class CriteriaPlacesInlineAdmin(admin.TabularInline):
model = PlacesToCriterias
form = CriteriaPlacesChoicesFieldForm  # <- ADDED FORM
class CriteriasAdmin(admin.ModelAdmin):
inlines = [CriteriaPlacesInlineAdmin]

admin.site.register(Criterias, CriteriasAdmin)


Form:

class CriteriaPlacesChoicesFieldForm(forms.ModelForm):
ct_place_type = ContentType.objects.get_for_model(PlaceTypesGroups)

object_id = forms.ModelChoiceField(PlaceTypesGroups.objects.all(), 
label='places')
content_type = forms.ModelChoiceField(ContentType.objects.all(), 
initial=ct_place_type, widget=forms.HiddenInput())

def clean_object_id(self):
return self.cleaned_data['object_id'].pk

def clean_content_type(self):
return self.ct_place_type

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/d6dd8a54-07f2-45b6-89bc-e92b1c56%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to replace content_type and object_id fields by a field with actual object in admin inline?

2016-10-14 Thread Tim Graham
Something like https://pypi.python.org/pypi/django-genericforeignkey? 
Search "django admin generic foreign key widget" for other possibilities.

On Friday, October 14, 2016 at 10:42:40 AM UTC-4, Anton Ponomarenko wrote:
>
> I have inline, which shows data of contenttype model, so instead of real 
> objects, I see content_type and object_id fields. I can exclude these 
> fields - this is not a problem, but also I want to get real current object 
> as selected with other Places in a dropdown list. Could anyone tell me, 
> how can I do this?
>
>
> Model:
>
> class Criterias(models.Model):
> name = ...
> class Places(models.Model):
> name = ...
> class PlacesToCriterias(models.Model):
> content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
> object_id = models.PositiveIntegerField()
> content_object = GenericForeignKey()
>
> criteria_group = models.ForeignKey(Criterias)
>
> Admin:
>
> class CriteriaPlacesInlineAdmin(admin.TabularInline):
> model = PlacesToCriterias
> class CriteriasAdmin(admin.ModelAdmin):
> inlines = [CriteriaPlacesInlineAdmin]
>
> admin.site.register(Criterias, CriteriasAdmin)
>
> I can add to CriteriaPlacesInlineAdmin a form, something like:
>
> class CriteriaPlacesChoicesFieldForm(forms.ModelForm):
> places = forms.ModelChoiceField(PlaceTypesGroups.objects.all(), 
> label='place')
>
> but how can I pass\add object_id to this form\query in order to get 
> 'selected' place in the dropdown list?
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b326e345-9682-45bb-bdd5-63c4c1699900%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.