A
A
Artem Matyashov2016-09-02 07:45:16
css
Artem Matyashov, 2016-09-02 07:45:16

How to auto-complete a field in TabularInline?

There is an application for managing scientific tournaments. Each user (except the superuser) has a ForeignKey for a model that describes a particular tournament.

class Tournament(models.Model):
    full_name     = models.CharField(max_length=NAME_LENGTH)
    short_name   = models.CharField(max_length=NAME_LENGTH)
    slug         = models.SlugField(max_length=SLUG_LENGTH, unique=True)
    description  = models.TextField(blank=True, null=True)
    opening_date = models.DateField(default=timezone.now)
    closing_date = models.DateField(blank=True, null=True)

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='ext_user_profile')
    tournament = models.ForeignKey('Tournament', blank=True, null=True)

It was decided to hide the tournament selection in the administrative panel for users to maintain integrity (so that the situation does not turn out that a team from one tournament belongs to another)
class Team(models.Model):
    tournament  = models.ForeignKey(Tournament)
    name           = models.CharField(max_length=NAME_LENGTH)
    slug             = models.SlugField(max_length=SLUG_LENGTH,
                                   null=True, blank=True)
    description = models.TextField(max_length=TEXT_LENGTH, blank=True)
    origin      = models.ForeignKey(TeamOrigin, null=True, blank=True)

А файле admin.py переопределил метод <b> save_model </b>:
@admin.register(models.Team)
class TeamAdmin(admin.ModelAdmin):
    fieldset = ['name']
    form = TeamForm
    inlines = [ParticipantInline]
    list_display = ['name', 'origin', ]
    def save_model(self, request, obj, form, change):
        if not request.user.is_superuser:
            if hasattr(request.user, 'ext_user_profile'):
                ext_user_profile = request.user.ext_user_profile
                if ext_user_profile.tournament:
                    obj.tournament = ext_user_profile.tournament
                else:
                    raise exceptions.PermissionDenied
        obj.save()

How can I make the same automatic assignment of the tournament in the admin.TabularInline derived class ?
class Participant(models.Model):
    tournament  = models.ForeignKey(Tournament)
    short_name  = models.CharField(max_length=NAME_LENGTH)
    full_name   = models.CharField(max_length=NAME_LENGTH, blank=True)
    origin      = models.ForeignKey(CommonOrigin, null=True, blank=True)
    grade       = models.CharField(max_length=GRADE_LENGTH, blank=True)
    team        = models.ForeignKey(Team)
    is_capitan  = models.BooleanField()

class ParticipantInline(admin.TabularInline):
    model    = models.Participant
    ordering = ["short_name"]
    form     = ParticipantForm
    extra    = 0

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question