I have these models :
class Filters(models.Model):
def __unicode__(self):
return format('%s' % self.title)
title = models.CharField(max_length=255,verbose_name='Name filter')
class FilterValue(models.Model):
value = models.CharField(max_length=255,verbose_name='value filter')
filter = models.ForeignKey(Filters)
class Casino(models.Model):
title = models.CharField(max_length=255,verbose_name='Name Casino')
filters = models.ManyToManyField(Filters)
In admin
class AdminCasino(admin.ModelAdmin):
fields= ['title','filters']
How can I get view in admin multiselect:
Name Filter
--value filter of this name
and so on
I'm not sure I am completely understanding your question but it sounds like you want tabular inlines.
Try adding the following to your admin
class FilterValueInline(admin.TabularInline):
model = FilterValue
class AdminCasino(admin.ModelAdmin):
inlines = [FilterValueInline]
Related
I have a model containing a ForeignKey to another model. I am attempting to serialize this model and want to control what field is returned for the foreignkey field. See below:
models.py
class Surveyor(models.Model):
num = models.CharField(max_length=3)
name = models.CharField(max_length=250)
class Anblsrecord(models.Model):
...
sur_num = models.ForeignKey(Surveyor, on_delete=models.CASCADE)
views.py
def anbls_points(request):
points_as_geojson = serialize('geojson', Anblsrecord.objects.all()[:5], fields=(... 'sur_num'))
return JsonResponse(json.loads(points_as_geojson))
When I view this I get:
... "sur_num": 1 ...
where the "1" is "num" from Surveyor class. I want to return "name".
I looked at https://docs.djangoproject.com/en/2.2/topics/serialization/ which talks about multi-table inheritance, but I can't find anything for a related table.
Any help would be greatly appreciated.
Django Rest Framework serializers with django-rest-framework-gis worked:
serializers.py
from anblsrecords import models
from rest_framework_gis.serializers import GeoFeatureModelSerializer
class AnblsrecordSerializer(GeoFeatureModelSerializer):
sur_name = serializers.CharField(source='sur_num.name')
class Meta:
model = models.Anblsrecord
geo_field = "geom"
fields = (
...
'sur_name',
)
views.py
from rest_framework import generics
class ListAnbls_points(generics.ListCreateAPIView):
queryset = Anblsrecord.objects.all()[:5]
serializer_class = serializers.AnblsrecordSerializer
This returns:
"properties": {
...,
"sur_name": "Name of Surveyor",...}, and includes the geometry feature.
So, I have three models , and there are connected with foreign key.
I'm using list_display on "avaria" , and I want to show the name of the "pavimento" inside of the model "avaria". What is the best options to that?
class pavimento(models.Model):
pavimento_nome = models.CharField("Pavimento",max_length=200)
class avaria(models.Model):
avaria_consumidores = models.IntegerField(blank=True,null=True,verbose_name="Consumidores")
class pavimentacao(models.Model):
pavimentacao_id=models.ForeignKey(avaria,related_name='avariaObjects',on_delete=models.CASCADE)
pavimentacao_avaria = models.ForeignKey(pavimento,on_delete=models.CASCADE, verbose_name="Pavimento")
You can write a property for avaria
#property
def pavimento_names(self):
pavimentacaos = self.avariaObjects.select_related('pavimentacao_avaria').values_list('pavimentacao_avaria__pavimento_nome', flat=True)
return ', '.join(pavimentacaos)
And add 'pavimento_names' both to readonly_fields and list_display lists of avaria admin.
Or you can directly add method into your admin like:
class AvariaAdmin(admin.ModelAdmin):
list_display = (..., 'get_pavimento_names')
def get_pavimento_names(self, obj):
pavimentacaos = obj.avariaObjects.select_related('pavimentacao_avaria').values_list('pavimentacao_avaria__pavimento_nome', flat=True)
return ', '.join(pavimentacaos)
get_pavimento_names.short_description = 'Pavimento Names'
get_pavimento_names.admin_order_field = 'avariaObjects__pavimentacao_avaria__pavimento_nome'
I have a simple model in django with a field using choices. As it is now, I am forced to use one of the available choices, how can I be able to use those choices and also be able to enter text manually?
WHERE_CHOICES = (
('CHL', 'Center HL'),
('CHD', 'Center HD')
)
class Event(models.Model):
ev_start = models.DateTimeField()
ev_end = models.DateTimeField()
where = models.CharField(max_length=3, default='CHL', choices=WHERE_CHOICES)
Thanks
You can override the admin form field widget with a datalist tag.
It would look like that (using the floppyforms library):
class EventAdminForm(ModelForm):
class Meta:
model = Event
fields = "__all__"
widgets = {
'where': floppyforms.widgets.Input(datalist=[v[0] for v in Event.WHERE_CHOICES])
}
class EventAdmin(admin.ModelAdmin):
form = EventAdminForm
admin.site.register(Event, EventAdmin)
Suppose I have the following model
class MyChoiceModel(models.Model):
mychoices = (('ChoiceA', 'ChoiceA'), ('ChoiceB', 'ChoiceB'))
and the following ModelForm
class MyChoiceModelForm(forms.ModelForm):
#...
class Meta:
model = MyChoiceModel
fields = ('mychoices', )
Now, the user can select all types of choices (ChoiceA and ChoiceB).
What I want now is that certain choice values won't be displayed.
How can I filter the available choices from mychoices such that for example only ChoiceA would be selectable by the user and - under other circumstances - only ChoiceB?
There are numerous ways to do this: here is a way that i have
def CustomChoiceList():
# return custom choices
class MyChoiceModelForm(forms.ModelForm):
class Meta:
widgets = { 'mychoices': CustomChoiceList() }
if you need more control, or access to the model look at creating a forms.ModelChoiceField)
eg:
class CustomChoices(forms.ModelChoiceField):
def label_from_instance(self, obj):
# return some obj.title, or whatever in your object as the label to show
then in the ModelForm
mychoices = CustomChoices(required=True, queryset=YourModelYouWant.objects.filter(...))
I have a simple model
class Professeurs(Metadata):
nom = models.CharField(max_length=255)
prenom = models.CharField(max_length=255)
biographie = models.TextField()
#photo = models.ForeignKey(image_model,blank=True)
photo = models.OneToOneField(Photo,primary_key=True,blank=True)
def __unicode__(self):
return "%s %s" % (self.nom, self.prenom)
which is registerd in the admin like that
class general(admin.ModelAdmin):
class Media:
js = ('/static/js/tiny_mce/tiny_mce.js', '/static/js/tiny_mce/textareas.js',)
class ProfesseursAdmin(general):
list_display = ('prenom' , 'nom' )
fields = ['prenom','nom', 'biographie','photo']
admin.site.register(Professeurs, ProfesseursAdmin)
In django admin section you can just click on your model to see all the instance you have created of this model. The problem is when i create some "professeur" they don't show up in the report of the professeur admin panel. But the count show "3" so it see there is some object. I can not understand why they dont show up
Here is a picture of the problem
https://dl.dropboxusercontent.com/u/14828537/Chose/Admin%20django.PNG
I found the problem. I did a migration and primary keys were changed because of this field
photo = models.OneToOneField(Photo,primary_key=True,blank=True). The primary_key=True was messing with the professeur.id