0

I have models like this:

class Devices(models.Model):
        name = models.CharField(max_length=255, blank=True)
        uniqueid = models.CharField(db_column='uniqueid'.lower(), max_length=255, blank=True) # Field name made lowercase.
        latestposition = models.ForeignKey('Positions', db_column='latestPosition_id'.lower(), blank=True, null=True) # Field name made lowercase.
        class Meta:
            db_table = 'devices'
            verbose_name = 'Devices'
            verbose_name_plural = verbose_name

        def __unicode__(self):
            return '%s' %(self.name)

        # Call the signal to create user device when the device is created.
        dispatcher.connect(save_user_device, signal=post_save, sender=Devices)


    class UsersDevices(models.Model):
        user = models.ForeignKey(settings.AUTH_USER_MODEL)
        devices = models.ForeignKey('Devices')
        class Meta:
            db_table = 'users_devices'
            verbose_name = 'User Devices'
            verbose_name_plural = verbose_name

        def __unicode__(self):
            return '%s %s' %(self.user, self.devices)

When the Devices is created, I want to create users devices. user field in the UsersDevices would be signed in user who created device and devices would be the device that was just created.

def save_user_device(sender, instance, **kwargs):
    ## Problem is here
    instance.UsersDevices.create(   )

How can I create a UsersDevices using this signal with the user instance and device instance

pynovice
  • 7,424
  • 25
  • 69
  • 109

2 Answers2

0

You don't really need signals in this case. Overwrite the save() method of the model:

def save(self, *args, **kwargs):

    # Call the original save function to actually save the model:
    super(Devices, self).save(*args, **kwargs)

    # Now this model is saved, so we can create the UsersDevices
    UserDevices(user=get_user_from_somewhere(), devices=self).save()

See the documentation for more information about overwriting the save() method:
https://docs.djangoproject.com/en/1.7/topics/db/models/#overriding-model-methods

gitaarik
  • 42,736
  • 12
  • 98
  • 105
  • Hm, I see. I have two questions: which is the best approach? And where do I get the `user instance` from? – pynovice Apr 10 '14 at 09:47
  • I added on a argument on `save()` method called `user` but while calling `form.save(request.user)` it throws error. – pynovice Apr 10 '14 at 09:54
  • @user2032220 I don't know where you should get the user from, it are YOUR users :P. And about `form.save()`, I don't know anything about your forms... maybe you should open another question for this? – gitaarik Apr 10 '14 at 10:06
  • I want to get the current logged in user. That's it. – pynovice Apr 10 '14 at 10:07
0

It is better to redefine save method of Devices model for this purpose. But if you want to use signals it might be done like this:

def save_user_device(sender, instance, **kwargs):
    ## Problem is here
    UsersDevices.create(devices=instance, user=instance.user)

In this case you have to add 'user' field to Devices model.

And small tip: you should give names to models singularly, plural names is bad style.

Eugene Soldatov
  • 9,755
  • 2
  • 35
  • 43