Superuser Access by Country

I am looking to provide superuser access to objects belonging to a specific country.

eg. Swedish SU can only control Swedish entities, etc.

However, I am new to django (taking on the old system) and I need a life cycle.

I would like to be able to specify a relationship table.

I already added userprofile and I have a new field called super_user_country_link = models.ForeignKey (SuperUserToCountry, blank = True, null = True)

and then under the new class

class SuperUserToCountry(models.Model):
    user = models.ForeignKey(User)
    country = models.ForeignKey(Country) 

I plan to run a script, then add an entry for each superuser and give them a link to country 0 (i.e. no country => su shared). Then I can delete these entries, as I start to enter entries specific to a specific country, so later I can call (using at home as an example):

if user.is_superuser:
    if user.get_profile().super_user_county_link.country == 0:
        #show house detail...
    elsif user.get_profile().super_user_county_link.country == 0
        #show house detail...
    else
        pass

Therefore, looking at this, this should mean that I can list several countries against one user, right? Maybe I changed my mind, but does it look right?

I come from a php background, so I just slightly doubt how correct this is ...

+3
source share
1 answer

, . , - UserProfile Country. , ManyToManyField. - :

class UserProfile(models.Model):
    countries = models.ManyToManyField(Country)

, (SuperUserToCountry), - . , , .

blank = True null = True . , (.. an_instance.countries.all() ).

, - :

profile = User.get_profile()
denmark = Country.objects.get(name = 'Denmark')
russia  = Country.objects.get(name = 'Russia')

if denmark in profile.countries.all():
    print "Something is rotten in the state of Denmark"
elsif russia in profile.countries.all():
    print "In Soviet Russia, profiles have countries!"

, , . , :

profile = User.get_profile()
denmark = Country.objects.get(name = 'Denmark')
profile.countries.add(denmark)
+1

Source: https://habr.com/ru/post/1760768/


All Articles