Prevent change of subject matter in the relationship

Asked

Viewed 26 times

0

Good afternoon, I’m trying to avoid altering an object when be part of a relationship, in my custom view.py worked out, but in the admin.py file an error occurs.. ha how to help me?

view.py

def altBloco(request, id):
    """
     verifica se o usuario tem perimissao, para fazer a operacao
    """
    if not request.user.has_perm('alocar.change_bloco'):
        return render(request, 'alocar/permissao1.html')

    bloco = get_object_or_404(Bloco, pk=id)
    form = AddBlocoForm(request.POST or None, request.FILES or None, instance=bloco)
    values = Sala.objects.select_related('bloco').filter(bloco__id=id)
    if form.is_valid():
        if values:
            messages.info(request, 'NÃO pode ser editado, já faz parte de algum relacionamento')
        else:
            form.save()
        return redirect('alocar:addbloco')

    return render(request, 'bloco/altbloco.html', {'form': form})


function in admin.py

@admin.register(Bloco)
class BlocoAdmin(admin.ModelAdmin):
    list_display = ('bloco',)
    search_fields = ('bloco',)
    ordering = ('bloco',)

    def save_model(self, request, obj, form, change):
        super(BlocoAdmin, self).save_model(request, obj, form, change)
        values = Sala.objects.select_related('bloco').filter(bloco__id=id)
        if form.is_valid():
            if values:
            messages.info(request, 'NÃO pode ser editado, já faz parte de algum relacionamento')
        else:
            form.save()

alterar bloco

  • Hi, I didn’t quite understand in the other post where the detail of your question was. Do you want to avoid editing an attribute? In models you can set Editable=False. Validate if this is what you need.

  • @Ernestocasanova.. Good evening...not only the attribute, but the whole object.. if already part of a relationship...can not be edited...can only be edited if it is not part of any relationship... for example "BLOCK A" has a relationship with "ROOM A100" so the same can not be edited, to be edited has to be free of

  • Okay, now I get it. Actually in the models it would be the place where you would define this and it would be available everywhere, even in the admin, this tag that I indicated will only do to the attribute and how fk and not to the models. In case of use, are you updating a flag in your models with Signals? Share the models so I can think of a solution, I think it will have to be a manager in the models.

1 answer

0

Hi, I’ve been analyzing and a simple solution with admin would be to add a flag to your models and when using these models you pass this flag to true, and in admin with an additional function you pass all attributes to read mode, not allowing edit as you wanted.

py.models

class Bloco(models.Model):
    ...
    already_taken   = models.BooleanField('Atribuido', default=False)

admin py.

@admin.register(Bloco)
class BlocoAdmin(admin.ModelAdmin):
    list_display = ('bloco',)
    search_fields = ('bloco',)
    ordering = ('bloco',)
    exclude=('already_taken',) # excluis a flag para controlo de atribuicao

    def get_readonly_fields(self, request, obj=None):
        if obj and obj.already_taken:
            return ['all_your_fields'] # não incluis already_taken, para não aparecer na form admin.
        else:
            return []

Browser other questions tagged

You are not signed in. Login or sign up in order to post.