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()

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.
– Ernesto Casanova
@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
– ElvisDba
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.
– Ernesto Casanova