Issue
Crontab works fine on simple task. e.g
def test():
test.objects.create.(name='Dino')
#It also works on this
def test_task()
if Users_Machine.objects.filter().exists():
test.objects.create(name='Dino')
but when i tried to query my db with foreignkey it does nothing.
Have tried this two methods:
#request method
def test_task(request):
if Users_Machine.objects.filter(user=request.user).exists():
test.objects.create(name='Dino', user=request.user)
#direct methods
def test_task()
if Users_Machine.objects.filter().exists():
name=Users_Machine.objects.get()
test.objects.create(name='Dino', user=name.username)
The Users_Machine.object returns True
Here is my models
#users_machine model
class Users_Machine(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
Name = models.CharField(max_length=20)
#test model
class test(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
Please how can i make this work?
Solution
It would be better if I get to look at User
model but for now I am going to assume in the request, you are passing Name
field which is present in the UserMachine
model and you can create new object in test
model as follows:
def test_task(request):
Current_user = User_Machine.objects.get(Name = request.name)
if Current_user:
test.objects.create(name='Dino', user=Current_user)
Can you please add User
model as well, that will allow me fine tune the answer (or correct it if this one is wrong)?
Answered By - Sourabh Burse Answer Checked By - Senaida (WPSolving Volunteer)