I'm trying to add an attribute to the object I've created in Django shell to create some queries but it says the object is not itrable

Viewed 20

I'm trying to make a project with django I've migrated the model successfully and when I want to add account number to my checkingaccount objects it says the object is not iterable everything else works properly(as I tested)

this is the models.py:

 from django.db import models

# Create your models here.

class Bank(models.Model):
    name = models.CharField(max_length=300)
    
    def __str__(self) -> str:
        return self.name

class BankAccount(models.Model):
    bank =            models.ForeignKey(Bank, on_delete=models.CASCADE)
    balance =         models.DecimalField(max_digits=18, decimal_places=2)
    account_number =  models.IntegerField()
    types = (
        ('c', 'checking account'),
        ('s', 'savings account'),
    )
    type_selection =  models.CharField('account type', max_length=1, choices=types )
    
    def __str__(self) -> str:
        return str(self.account_number)

class CheckingAccount(models.Model):
    balance =           models.DecimalField(max_digits=18, decimal_places=2)
    date_created =      models.DateField()
    last_transaction =  models.DateField()
    account_number =    models.ManyToManyField(BankAccount)
    
    def __str__(self) -> str:
        return self.account_number
    
    def deposit(cash, self):
        self.balance += cash
    
    def check_balance(self):
        return self.balance
    
    def withdrawal(self, cash):
        self.balance -= cash

class SavingsAccount(models.Model):
    balance =         models.DecimalField(max_digits=18, decimal_places=2)
    date_created =    models.DateField()
    account_number =  models.ManyToManyField(BankAccount)
    
    def __str__(self) -> str:
        return self.account_number
    
    def deposit(cash, self):
        self.balance += cash
    
    def check_balance(self):
        return self.balance

this is the shell input and output:

>>> acC.account_number.set(ac)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\fields\related_descriptors.py", line 992, in set
    objs = tuple(objs)
TypeError: 'BankAccount' object is not iterable

any helps appreciated =)

0 Answers
Related