These are chat archives for django/django

10th
Jun 2018
Derek
@DErekXx
Jun 10 2018 09:30

hi everyone.
please help me.
i have model with:

 unique_together = (("user", "date"),)

and ModelViewSet with:

            for item in validated_data:
                item["user"] = request.user

            UserActivity\
                .objects\
                .on_conflict([('user','date')], ConflictAction.NOTHING)\
                .bulk_insert(validated_data)

i got the message:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'User'

but if i pass user.id, i got the message

Cannot assign "9": "UserActivity.user" must be a "User" instance.

Where is problem ?
How fix is there ?

neutralboy
@neutralboy
Jun 10 2018 09:58
Have you actually passed a User Field in the form?
Also insert User ID instead of the user object that should solve the problem..
Derek
@DErekXx
Jun 10 2018 10:05
yes. i passed user.id
but got the message
Cannot assign "9": "UserActivity.user" must be a "User" instance.
neutralboy
@neutralboy
Jun 10 2018 10:08
Post your whole model here
Derek
@DErekXx
Jun 10 2018 10:11
class UserActivity(PostgresModel):
    id = models.AutoField(primary_key=True)
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    created_at = models.DateTimeField(auto_now_add=True)
    date = models.DateTimeField(default=datetime.now, blank=True)
    heart_rate = models.IntegerField(default=0, blank=True)
    intensity = models.IntegerField(default=0, blank=True)
    steps = models.IntegerField(default=0, blank=True)
    type = models.ForeignKey(UserActivityType, blank=False, default=0, on_delete=models.PROTECT)

    def __str__(self):
        return str(self.id)

    class Meta:
        db_table = 'user_activity'
        unique_together = (("user", "date"),)