How to implement device registration of a device in a Django user account

I am currently working on a website that will allow the user to interact with a hardware unit that they can set up in their home. I am currently registering my account with django-allauth and this works as desired. My next step is to let the user associate their hardware unit with their account. I hope to implement this using some kind of unique character key (eg Sr3D- $ 3tr-SQ2Q-CB24) that can only be associated with one account. I don't want this to be part of the User model (for registration), rather something that they can bind later, for example in the profile model (associated with the user model).

I've searched extensively for django packages or examples that implement similar functionality, but no luck. I find this to be pretty standard functionality, such as when you need to register the game you are buying (to access online features) using the passphrase that comes with your purchase.

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

[EDIT] After BingsF's first answer below, I thought I should be more precise about the functionality I am doing. Basically I would like to have a table in the database where I could add legitimate keys along with the username of the user who logs it in (initially "Null" until the user logs in with the key). Then, when the user signs up, I need a validation to make sure they entered a legitimate key and it hasn't been registered yet.

The user must be able to register multiple devices.

+3


source to share


1 answer


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

It will 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 web page, when the user uploads a new dongle, you will 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

      

For more information on creating and using models, see here .

+2


source







All Articles