I have created 2 Django models, User and Account Model. In User model, i have all info and in account part, i have account details of that user. I have designed Views that when I create a user, respective account object also created. URLs: '/user/' will create new user and account object.
but when I am writing tests to check APIs, I created a new user. User object is accessible but when I access account detail it throws models.object doesn't exist.
class User(models.Model):
user_name = models.CharField(max_length=35)
first_name = models.CharField(max_length=35)
last_name = models.CharField(max_length=35)
email = models.EmailField(max_length=255)
class Account(models.Model):
user = models.ForeignKey(User, on_delete = models.CASCADE)
balance = models.IntegerField(default = 0)
def post(self, request):
received_json_data = json.loads(request.body.decode("utf-8"))
user = User(user_name=received_json_data['user_name'], first_name=received_json_data['first_name'],
last_name=received_json_data['last_name'], email=received_json_data['email'])
user.save()
account = Account(user=user);
account.save();
class TestUser(TestCase):
def setUp(self):
self.c = Client()
user1 = {"email": "ram@gmail.com", "user_name": "ramu", "first_name": "ramesh", "last_name": "yadav"}
data1 = json.dumps(user1)
self.response = self.c.post('/user/', data1, content_type="application/json")
self.assertEqual(self.response.status_code, 200)
account = Account.objects.get(pk =1)
Aucun commentaire:
Enregistrer un commentaire