简体   繁体   中英

Django - build a query with these models

The goal is:

Admin logs in to see a list of all registered members in his association. Admin does not want to see members who are in other associations but only in his association.

Example:

Coach (Admin) want to see all of his players in Real Madrid (association) and are not interested in seeing other soccer clubs (associations) players in his team.

I have tried for a long time and ran out of ideas, so hopefully you guys have some great ideas i can try =D

Still a newbie so appreciate your help!

admin\\models.py

class Administrator(AbstractUser):
    ...
    asoc_name = models.CharField(max_length=100)


    class Meta:
        db_table = 'Administrator'

member\\models.py

from pl.admin.models import Administrator


class Member(models.Model):
    member_no = models.AutoField(primary_key=True)
    asoc_name = models.CharField(max_length=100)
    ...

    class Meta:
        db_table = 'Member'


class Association(models.Model):
    asocnumber = models.AutoField(primary_key=True)
    asoc_name = models.CharField(max_length=100)
    user = models.ForeignKey(Administrator)
    member_no = models.ForeignKey(Member)

    class Meta:
        db_table = 'Association'

views.py

 def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if not form.is_valid():
            return render(request, 'admin/signup.html',
                          {'form': form})

        else:
            ...
            asoc_name = form.cleaned_data.get('asoc_name')
            ...
            Administrator.objects.create_user(...
                                              asoc_name=asoc_name,    
                                              ...)
            user = authenticate(...
                                asoc_name=asoc_name,
                                ...)
            return redirect('/')

    else:
        return render(request, 'admin/signup.html',
                      {'form': SignUpForm()})

Let me know if you need more info!

Ok, so you're linking these models up using CharFields, which can technically work, but you probably want to use ForeignKeys .

Potentially, this means having code that looks like:

class Administrator(AbstractUser):
    ...
    asoc = models.ForeignKey(Association)


    class Meta:
        db_table = 'Administrator'

class Member(models.Model):
    member_no = models.AutoField(primary_key=True)
    asoc = models.ForeignKey(Association)
    ...

    class Meta:
        db_table = 'Member'


class Association(models.Model):
    asocnumber = models.AutoField(primary_key=True)
    asoc_name = models.CharField(max_length=100)
    member_no = models.ForeignKey(Member)

    class Meta:
        db_table = 'Association'

You can then make the kinds of queries you want. Example: given an administrator user, get all the members of his assosiation.

Member.objects.filter(asoc=admin.asoc)

Edit

Ok, when you create the Administrator user you need to pass in an association. One way to do this is to have selecting the association be a part of the sign up process. So for example you could add a ModelChoiceField to your sign up form, or something like that, or have the admin be placed in a default association. But the idea is that you have an association object ready when you then create the user, and you pass it in to Administrator.create_user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM