简体   繁体   中英

How to implement hardware device registration to a Django user account

I am currently working on a website that will allow the user to interact with a hardware unit they can setup in their home. I am currently doing my user registration using django-allauth and this works as desired. My next step is to allow the user to link their hardware unit to their account. I hope to implement this using some sort of unique key made up of characters (eg Sr3D-$3tr-SQ2Q-CB24) that can only be linked to a single account. I don't want this to be part of the User model (for registration), rather something they can link later on in say a Profile model (linked to the User model).

I have searched extensively for django packages or examples that implement similar functionality however have had no luck. I believe this to be pretty standard functionality, for example when you have to register a game you buy (to access online functionality) using the key-phrase that comes with your purchase.

Can anybody point me to some examples or packages of how this would be implemented using Django?

[EDIT] After BingsF's first answer below I thought I should be more precise in the functionality I am after. Basically I would like to have a table in a database where I could add legitimate keys along with the username of the user who registers it (initially 'Null' until a User registers themselves with the key). Then when the user registers I would want a check to ensure they have entered a legitimate key and it hasn't been registered before.

A user should be able to register multiple devices.

My approach would probably be to create a new Model, with a foreign key referencing the User model.

It would look something like this (in your models.py):

class Device(models.Model):
    username = models.ForeignKey(User)
    hardware_key = models.CharField(max_length=32, primary_key=True)

Then, on your webpage, when a user uploads a new hardware key you would do:

from models import Device, User

try:
    device = Device.objects.get(hardware_key=<key>)
except ObjectDoesNotExist:
    # deal with case where user tries to register non-existent key
else:
    try:
        device.username = <user>
        device.save()
    except IntegrityError:
        # deal with case where key is already registered to another user

See here for more info on creating/using models

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