0
class Consumer (models.Model):
     name=models.CharField(max_length=32)
     address=models.CharField(max_length=32)
     telephone=models.CharField(max_length=32)
     email=models.CharField(max_length=32)
     ac_no=models.IntegerField(default=0)
class Consumer_order(models.Model):
     name=models.ForeignKey(Consumer, on_delete=models.CASCADE)
     ac_no=models.ManyToManyField(Consumer)
     newspaper=models.ForeignKey(Newspaper, on_delete=models.CASCADE)
     added_date=models.DateField(max_length=32,auto_now_add=True)

i try many ways many to many relationship but not work those error occur (fields.E302,E303,E304). How to get Model Consumer from ac_no To Consumer_order to ac_no datafield?

ac_no not show integer value

Ravi
  • 1
  • 1
  • 5

2 Answers2

0

I think if i understood well your question, you should add related_name parameter to the field . i think this problem apear because you call the same model twice into your Consumer_order model .

    name=models.ForeignKey(Consumer,related_name="Consumer_name", on_delete=models.CASCADE)
     
    ac_no=models.ManyToManyField(Consumer,related_name="Consumer_ac_no")

Note if you want to acess to ac_no of Consumer model from Consumer_order model you shoud have like this is exemple :

 #to get specific object consumer_order in this example I choose object with specific date ,you can crate it ,if you want loop throught all objetcs use objects.all() , you can also directly use .filter : Consumer_order.objects.filter(added_date='2019-11-28').first()
 consumer_order =Consumer_order.objects.get(added_date='2019-11-28')
 #to get all objects consumer into the filed ac_no in Consumer_order model
 ac_no_all =consumer_order.ac_no
 # to loop throught all the consumer object and print ac_no intger
 for ac_no_ in ac_no_all:
     print(ac_no_.ac_no)
  • it is can add to data. but ac_no data field not show integer value. it is show same name data field name – Ravi Aug 07 '20 at 13:28
  • can you show me please how you get the data from ac_no of Consumer_order? and if you can change name ac_no of Consumer_order to ac_no_order . just to make debuging easy . – ahmed barbouche Aug 07 '20 at 13:38
  • **Here i attach code** https://github.com/Ravindrakumara/Newspaper_tracking – Ravi Aug 07 '20 at 13:50
  • click above this link i attach screen shot **ac_no not show integer value** – Ravi Aug 07 '20 at 13:54
  • sorry i didnt see the eror screen just i get link to the project – ahmed barbouche Aug 07 '20 at 13:58
  • in where have to put this Consumer_order.ac_no.ac_no? i try but did not work – Ravi Aug 07 '20 at 15:19
  • sorry i didnt explain clearly , i edit my response :) ihope its help you – ahmed barbouche Aug 07 '20 at 17:16
  • Note in your GitHub repo you should add gitgnore file – ahmed barbouche Aug 08 '20 at 00:56
  • is it possible to insert data Models (A) to Models (B) for every day automatically – Ravi Aug 10 '20 at 03:53
  • there s 2 part of your question : insert data && evry day automaticly . for the part of insert i think you know now how you do . and for the part of make it automaticly you can use celery https://stackoverflow.com/questions/23563934/django-how-to-run-a-function-everyday – ahmed barbouche Aug 10 '20 at 08:56
  • For the part one, i think you mean insert data from Consumer_order to Consumer yes . you can use .add() https://stackoverflow.com/questions/1182380/how-to-add-data-into-manytomany-field – ahmed barbouche Aug 10 '20 at 08:59
  • def save(self, *args, **kwargs): scheduler = BackgroundScheduler() @periodic_task(run_every=crontab(minute=30, hour='7', day_of_week='mon,tue,wed,thu,fri,sat,sun')) self.ac_no = self.Consumer_order.ac_no self.newspaper = self.Consumer_order.newspaper super(Daily_Cart, self).save(*args, **kwargs) – Ravi Aug 10 '20 at 12:04
  • i code this every task. it not work. my mind stuck do not know – Ravi Aug 10 '20 at 12:06
  • Please open new issue ! – ahmed barbouche Aug 11 '20 at 09:09
0

You can use the related_name

class Consumer(models.Model):
     name=models.CharField(max_length=32)
     address=models.CharField(max_length=32)
     telephone=models.CharField(max_length=32)
     email=models.EmailField(max_length=32)
     ac_no=models.CharField(blank=True,max_length=254)

     def __str__(self):
         return self.name

class Consumer_order(models.Model):
     name=models.ForeignKey(Consumer,on_delete=models.CASCADE,related_name='consumer')
     ac_no=models.ManyToManyField(Consumer)
     newspaper=models.ForeignKey(Newspaper, on_delete=models.CASCADE,related_name='xyz')
     added_date=models.DateField(max_length=32,auto_now_add=True)
Ravi
  • 28
  • 1
  • 9
  • ac_no means Account number so should have to integer. may i know what is the reason has to change char field? – Ravi Aug 07 '20 at 15:17
  • @Ravi account number is consider as varchar so i use CharField but if you require integer field then use models.BigAutoField. – Ravi Aug 10 '20 at 04:24
  • @Ravi BigAutofield is not working or you not able to add account number in your admin template in your consumer_order ??? – Ravi Aug 10 '20 at 07:35
  • @Ravi If your consumer_order table in acc_no. not showing then [`follow this stack question`](https://stackoverflow.com/questions/163823/can-list-display-in-a-django-modeladmin-display-attributes-of-foreignkey-field) – Ravi Aug 10 '20 at 07:39