Django admin form, field instead of object in foreign key - python

Im using a forigen key to reference another object from my parent object. However when i go to the drop down list created by django admin, i get the object name instead of the field value. how can i add the field value to the form instead?
admin.py
from django.contrib import admin
from .models import Maintenance
from .models import MaintenanceType
from .models import ServiceType
# Register your models here.
class MaintenanceAdmin(admin.ModelAdmin):
list_display = ('Title','Impact','Service','Description','StartTime','EndTime',)
list_editable = ('Title','Impact','Service','Description','StartTime','EndTime',)
admin.site.register(Maintenance, MaintenanceAdmin)
class MaintenanceTypeAdmin(admin.ModelAdmin):
list_display = ('Type',)
list_editable = ('Type',)
admin.site.register(MaintenanceType, MaintenanceTypeAdmin)
class ServiceTypeAdmin(admin.ModelAdmin):
list_display = ('Service','Service',)
list_editable = ('Service','Service',)
admin.site.register(ServiceType, ServiceTypeAdmin)
models.py
from django.db import models
# Create your models here.
class MaintenanceType(models.Model):
Type = models.CharField(max_length=200)
class Meta:
verbose_name = "Planned Maintenance Types"
verbose_name_plural = "Planned Maintenance Types"
class ServiceType(models.Model):
Service = models.CharField(max_length=200)
class Meta:
verbose_name = "Service Types"
verbose_name_plural = "Service Types"
class Maintenance(models.Model):
Title = models.CharField(max_length=200)
Impact = models.ForeignKey(MaintenanceType)
Service = models.ForeignKey(ServiceType)
Description = models.TextField()
StartTime = models.DateTimeField()
EndTime = models.DateTimeField()
class Meta:
verbose_name = "Planned IT Maintenance"
verbose_name_plural = "Planned IT Maintenance"

Implement __str__ in the MaintenanceType model, which should return a string in whatever formatting you wish to appear in the drop down (and anywhere else actually).
It appears that you simply need to return self.Type.

Python 2 only
You can specify what's returned by accessing an object by setting the unicode method to retun what you want it to be.
So I think your MaintenanceType should look like
class MaintenanceType(models.Model):
Type = models.CharField(max_length=200)
class Meta:
verbose_name = "Planned Maintenance Types"
verbose_name_plural = "Planned Maintenance Types"
def __unicode__(self):
return self.Type

Related

Sort a displayed column defined by a custom model method in the Django admin interface

I want to be able to sort a table column defined using a custom method in the Django admin.
I narrowed down the problem to this simple example in Django:
models.py:
from django.db import models
class MyObject(models.Model):
name = models.CharField(_("name"), max_length=255)
layers = models.URLField(_("Layers"), blank=True, max_length=1024)
choices = models.TextField(
verbose_name=_("Choice values"),
blank=True,
help_text=_("Enter your choice"),
)
class Meta:
verbose_name = _("Object config")
verbose_name_plural = _("Objects config")
def __str__(self): # my custom method
return self.name
and admin.py:
from django import forms
from django.contrib import admin
class MyObjectAdminForm(forms.ModelForm):
"""Form"""
class Meta:
model = models.MyObject
fields = "__all__"
help_texts = {
"layers": "URL for the layers",
}
class MyObjectAdmin(admin.ModelAdmin):
form = MyObjectAdminForm
list_filter = ["name",]
search_fields = ["name",]
# I want the first column (__str__) to be sortable in the admin interface:
list_display = ["__str__", ...] # the ... represent some other DB fields
but for the moment I cannot sort that first column (it is grayed out, I cannot click on its title):
So how could I sort the first column in this admin table as defined by the __str__() method of the MyObject model? (please note that I cannot change the model itself. I'm also brand new to Django, so don't hesitate to detail your answer as if you were speaking to a kid.)

How can I create form using this django model?

I am trying to create form using this model. I want to add data in this database model using form to perform CRUD operation. I am using MySQL database.
models.py
from django.db import models
from .managers import CategoryManager, SubCategoryManager
# this is my parent model
class Node(models.Model):
name = models.CharField(max_length=150)
parent = models.ForeignKey(
'self',
on_delete=models.CASCADE,
related_name='children',
null=True,
blank=True
)
def __str__(self):
return self.name
class Meta:
ordering = ('name',)
verbose_name_plural = 'Nodes'
class Category(Node):
object = CategoryManager()
class Meta:
proxy = True
verbose_name_plural = 'Categories'
class SubCategory(Node):
object = SubCategoryManager()
class Meta:
proxy = True
verbose_name_plural = 'SubCategories'
class Product(models.Model):
sub_category = models.ForeignKey(
SubCategory, on_delete=models.CASCADE
)
name = models.CharField(max_length=100)
description = models.TextField(blank=True)
def __str__(self):
return self.name
Try the Imagine smart compiler which allows automatic generating of code + tests for your CRUD APIs and Django models from a very simple config. Amongst other things, it generates code in the correct way to handle foreign key relationships in Django Views. You can also try a demo here imagine.ai/demo
PS: Something like this simple config will generate all the code for the CRUD API along with the tests!
Model Node {
id integer [primary-key]
name string [max-length 150]
}
Model Product {
id integer [primary-key]
name string [max-length 100]
description string [nullable]
}

Display an inherited Field in Django ModelAdmin

I have models.py file as follows.
from django.db import models
class UpdateCreateModelMixin:
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Question(UpdateCreateModelMixin, models.Model):
name = models.CharField(max_length=100)
code = ...
... (Some more field)
How do I display the updated, created fields in the Django model Admin, my admin.py file is
from django.contrib import admin
from .models import Question
class QuestionAdmin(admin.ModelAdmin):
list_display = ('name', 'created', 'updated')
list_filter = ('created')
admin.site.register(Question, QuestionAdmin)
For the above admin.py file I am getting this error.
<class 'assignments.admin.QuestionAdmin'>: (admin.E116) The value of 'list_filter[0]' refers to 'created', which does not refer to a Field.
So, how do I add an inherited fields in the django model Admin and why is the above failing?
class UpdateCreateModelMixin(models.Model):
class Meta:
abstract = True
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Question(UpdateCreateModelMixin):
name = models.CharField(max_length=100)
code = ...
... (Some more field)

Use of foreignKey between app models in Django 1.10

I have a website built in Django 1.10. The site has 3 different apps: teams, members and news.
The first app, called teams has one model called Team.
This is the Team/models.py:
from django.db import models
from django.db.models.signals import pre_save
from django.utils.text import slugify
class Team(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
slug = models.CharField(max_length=255, default='team', editable=True)
class Meta:
ordering = ('name',)
def __unicode__(self):
return self.name
The second app, called members has one model called Member.
This is the Member/models.py:
from django.db import models
class Piloto(models.Model):
name = models.CharField(max_length=255)
biography = models.TextField()
slug = models.CharField(max_length=255, default='piloto', editable=True)
class Meta:
ordering = ('name',)
def __unicode__(self):
return self.name
What I want is include the name of the team inside the member profile, so I know it should be something like:
team_of_member = models.ForeignKey();
But I don't know what to put in the parenthesis or how to import the model of the team to the model of the member. I was following the documentation of Django 1.10 but it wasn't working, also I've tried this link but it doesn't work. Could you give a hand? Thanks
Edit:
I tried to do as #Bulva was suggesting, so my code is now like this:
from django.db import models
from equipos.models import Team
class Member(models.Model):
name = models.CharField(max_length=255)
team = models.ForeignKey('teams.Team', null=True)
biography = models.TextField()
slug = models.CharField(max_length=255, default='piloto', editable=True)
class Meta:
ordering = ('name',)
def __unicode__(self):
return self.name
What you want is to provide the teams the member is a member of. It all depends on your business logic. A team has many members by definition, but can a member be a part of many teams? If yes, you have a many-to-many relationship. If no, you have a one-to-many relationship.
Under one-to-many assumption, the foreignkey information has to be put in the referenced model. Then:
from django.db import models
from team.models import Team # Generally, apps are in all lower-case (assuming your app is called team)
class Member(models.Model):
name = models.CharField(max_length=255)
team = models.ForeignKey('team.Team', related_name = 'members', null=True) # Do not forget to put team.Team inside a pair of single-quotes.
biography = models.TextField()
slug = models.CharField(max_length=255, default='piloto', editable=True)
class Meta:
ordering = ('name',)
def __unicode__(self): # use def __str__(self): in Python 3+
return self.name
In your view, you can then say this:
albert_team = albert.team
albert_teammates = albert_team.members
Under Many-to-Many assumption, it is more natural to capture the relationship in the team model:
from django.db import models
from django.db.models.signals import pre_save
from django.utils.text import slugify
class Team(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
slug = models.CharField(max_length=255, default='team', editable=True)
members = models.ManyToManyField('team.Member', related_name = 'teams')
class Meta:
ordering = ('name',)
def __unicode__(self):
return self.name
In views.py:
albert_teams = albert.teams
all_albert_teammates = []
for team in albert_teams:
all_albert_teammates.append(team.members)
Try this:
from teams.models import Team
# in the member model field
team_of_member = models.ForeignKey(Team);
In order to do what you want (assuming the code provided is your full "problematic" code) you should:
In Member/models.py:
from django.db import models
from teams.models import Team # <--add this line
class Piloto(models.Model):
name = models.CharField(max_length=255)
team_of_member = models.ForeignKey(Team, on_delete=models.CASCADE); # <--add this line
biography = models.TextField()
slug = models.CharField(max_length=255, default='piloto', editable=True)
class Meta:
ordering = ('name',)
def __unicode__(self):
return self.name
After you do this, remember to:
python manage.py makemigrations
python manage.py migrate
to change your models state.
Good luck :)

How to make Django admin shows OneToMany instead of ForeignKey

In Django admin page when we use a ForeignKey in a object, the admin shows the option to set a ForeignObject in the model that has the ForeignKey value.
For example:
class Diferencial(SobreFather):
class Meta:
verbose_name = 'Diferencial'
verbose_name_plural = 'Diferenciais'
class DiferencialItem(models.Model):
diferencial = models.ForeignKey(Diferencial)
icone = models.ImageField(upload_to="icones_diferencial")
texto = models.CharField(max_length=50, null=False, blank=False)
def __unicode__(self):
return self.diferencial
class Meta:
verbose_name = 'Item Diferencial'
verbose_name_plural = 'Itens Diferencial'
This code will show the option to select a Diferencial object to be related to DiferencialItem in django admin.
Is there a way to show the option to create the DiferencialItem in the Diferencial admin view?
Yes, use the inline model admin:
class DiferencialItemInline(admin.TabularInline):
model = DiferencialItem
class DiferencialAdmin(admin.ModelAdmin):
inlines = [DiferencialItemInline]

Categories

Resources