Django权限行表给确定的用户

时间:2019-06-02 21:38:33

标签: django django-models

我在问有关权限的问题,我有2个表,AnagraficaCliente和与1关联的跟踪到许多,默认情况下,用户的autentichate内置了django.contrib.auth.urls。 我搜索了许多论坛和站点,但我不了解如何为确定的用户设置行权限。例如: 我的网站是一个用于显示运送物品的网络跟踪,如果我在跟踪中搜索了它起作用的用户,但是所有跟踪都可见(对于所有用户),我希望只有属于用户的跟踪才能显示给他。 我认为我使用

的模型用户

django.contrib.auth.models导入用户

我不知道如何使用我的代码。

完全感谢。

models.py

from django.db import models
from django.urls import reverse

# Create your models here.

class AnagraficaCliente(models.Model):
    codice_cliente = models.CharField(max_length=20, primary_key=True, null=False, unique=True)
    ragione_sociale = models.CharField(max_length=80)
    #ragione_sociale_dest = models.CharField(max_length=40)
    nome = models.CharField(max_length=40, blank=True)
    cognome = models.CharField(max_length=20, blank=True)
    #ragione_sociale = models.CharField(max_length=20)
    indirizzo = models.TextField(blank=True)
    cap = models.CharField(max_length=5, blank=True)
    piva = models.CharField(max_length=20, blank=True)
    vatnumber = models.CharField(max_length=20, blank=True)
    #ragione_sociale_dest = models.CharField(max_length=40)
    #indirizzo_dest = models.TextField(null=True)

    def __str__(self):
        #return self.ragione_sociale + " " + self.codice_cliente
        #return self.ragione_sociale_dest + " - " + self.indirizzo_dest + " - " + self.codice_cliente 
        return self.codice_cliente + " - " + self.ragione_sociale

    class Meta:
        verbose_name = "AnagraficaCliente"
        verbose_name_plural = "AnagraficaClienti"




class Tracking(models.Model):
        track = models.CharField(max_length=11, null=False, unique=True, primary_key=True)
        indirizzo_dest = models.TextField(null=False)
        passaggio1 = models.CharField(max_length=50, blank=True)
        data1 = models.DateField(blank=True, null=True) 
        passaggio2 = models.CharField(max_length=50, blank=True)
        data2 = models.DateField(blank=True, null=True)
        passaggio3 = models.CharField(max_length=50, blank=True)
        data3 = models.DateField(blank=True, null=True)
        passaggio4 = models.CharField(max_length=50, blank=True)
        data4 = models.DateField(blank=True, null=True)
        passaggio5 = models.CharField(max_length=50, blank=True)
        data5 = models.DateField(blank=True, null=True)
        consegna = (
        ('C', 'Consegnato'),
        ('N', 'Non consegnato'),
        )
        consegnato = models.CharField(
            max_length=1, choices=consegna, blank=True, null=True)

        #consegnato = models.BooleanField(blank=True)
        #esito = models.CharField(max_length=10, blank=True)
        flag = models.CharField(max_length=2, blank=True)
        AnagraficaCliente = models.ForeignKey(AnagraficaCliente, on_delete=models.CASCADE, related_name='trackings')

        def __str__(self):
            return self.track

        class Meta:
            verbose_name = "Tracking"
            verbose_name_plural = "Trackings"

admin.py

from django.contrib import admin
from .models import AnagraficaCliente, Tracking
from import_export.admin import ImportExportModelAdmin
#from import_export.admin import ImportExportActionModelAdmin
from import_export import resources

# Register your models here.


class ClientResource(resources.ModelResource):
    class Meta:
        model = AnagraficaCliente
        skip_unchanged = True
        report_skipped = True
        #fields = ('id','codice_cliente','ragione_sociale','nome','cognome','ragione_sociale','indirizzo','cap','piva','vatnumber')
        import_id_fields = ['codice_cliente']


@admin.register(AnagraficaCliente)#Decoratore per mostrare in admin anagraficacliente
class SearchCodice(admin.ModelAdmin):
    search_fields = ('codice_cliente', 'ragione_sociale')#Ricerca in admin
    list_display = ('codice_cliente','ragione_sociale','indirizzo')#Elenco nomi campo nelle tabelle

class ClientAdmin(ImportExportModelAdmin):
    resource_class = ClientResource #Classe per import export csv punta alla classe ClientResource



class ClientResource(resources.ModelResource):
    class Meta:
        model = Tracking
        skip_unchanged = True
        report_skipped = True
        #fields = ('id','codice_cliente','ragione_sociale','nome','cognome','ragione_sociale','indirizzo','cap','piva','vatnumber')
        import_id_fields = ['track']


@admin.register(Tracking)
class SearchTrack(admin.ModelAdmin):
    search_fields = ('track', 'consegnato')
    list_display = ('track','indirizzo_dest','consegnato')

1 个答案:

答案 0 :(得分:-1)

好,我是这样解决的:

在模型中,我添加了一个新字段,其中将包含用户的确切名称(请参见注释):

user = models.CharField(max_length=20, blank=True)

在查询对象的视图中,我更改了这一行:

def tracking_views(request):
query = request.GET.get('q', None)
context = {}
#query_anag = AnagraficaCliente.objects.all()

if query and request.method == 'GET':
    #results = Tracking.objects.filter(track=query)#Old line, see next line
    results = Tracking.objects.filter(track=query, user=request.user) #Line that 
    changed
    print (type(results))
    if results.exists():
        context.update({'results': results})
        print(context)
    else:
        return render(request,'tracking_not_found.html',context)
        # return HttpResponse("<h1>post creato con successo!</h1>")
return render(request,'tracking.html',context)

因此,当用户通过身份验证时,只会显示具有属于该用户的跟踪号的查询(当然,在每条记录中,都需要将附加信息的行映射到新字段“用户”)