logo
down
shadow

Import app foreign key model class in another app model


Import app foreign key model class in another app model

By : user2954903
Date : November 22 2020, 10:33 AM
With these it helps put db_table to your model and move it wherever you want. it refers then always to that old db table and you dont need any migrations for your new app.
code :
class Product(models.Model):
   class Meta: 
     db_table = 'oldapp_tablename'


Share : facebook icon twitter icon
Django import error from foreign key in another application model

Django import error from foreign key in another application model


By : Hadique
Date : March 29 2020, 07:55 AM
I wish did fix the issue. My guess is that you have created a circular import condition. This occurs when you import something from one python module which in turns imports from the module which is trying to import it, thus preventing the import from ever resolving.
In general there are three strategies for dealing with circular imports, two of which will work in this case:
Import app model class in another app model

Import app model class in another app model


By : asn_177
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I got 2 app: coworkers and services, each one with its own models.py , In Django 1.6.5 this:
code :
import coworkers
status = models.ForeignKey(coworkers.models.Status)
import coworkers
status = models.ForeignKey(coworkers.Status)
How to bind Foreign Key? How to create Model objects with foreign key in controller class?

How to bind Foreign Key? How to create Model objects with foreign key in controller class?


By : saberonline
Date : March 29 2020, 07:55 AM
wish help you to fix your issue First thing, I would like to suggest to use EF code conventions instead of explicitly defining the relationships in EF (Your declaration is correct , it's just a personal preference for me and i think it's cleaner).
So instead of this:
code :
    //ForeignKey("<Navigation Property Name>")
    [Key, ForeignKey("ProfileMeta")] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
    public int ProfileMetaID {get; set;}
    public virtual ProfileMeta ProfileMeta { get; set; }
 public int ProfileMetaID { get; set; }
 public virtual ProfileMeta profileMeta { get; set; }
public class ProfileDetail
    {

        [Key, DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        public string UserName { get; set; }
        public string Age { get; set; }
        public string Location { get; set; }
        public string Gender { get; set; }
        public int ProfileMetaID { get; set; }
        public virtual ProfileMeta profileMeta { get; set; }

    }
    public class ProfileMeta
    {

[Key, DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
            public int ID { get; set; }
            public string Username { get; set; }
            public string password { get; set; }
            public virtual ProfileDetail profileDetail {get; set;}
        }
[HttpGet]
        public ActionResult Create()
        {
            return View(new ProfileMeta());
        }
     [HttpPost]

     [ValidateAntiForgeryToken]
         public ActionResult Create(Register register)
            {
                if (ModelState.IsValid)
                 {            
                    db.ProfileMetas.Add(profileMeta);
                    db.ProfileDetails.Add(profileDetail);
                    db.SaveChanges();
                 }
           }
 <div class="form-group">
            @Html.LabelFor(model => model.Username, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Username, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.profileDetail.Age, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.profileDetail.Age, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.profileDetail.Age, "", new { @class = "text-danger" })
            </div>
        </div>
Sequence in the django model access between model through foreign key to model how to fix my models?

Sequence in the django model access between model through foreign key to model how to fix my models?


By : RobMalt
Date : March 29 2020, 07:55 AM
I wish this helpful for you I have such a problem that can be turned into a lesson, a hack, with models, and with url-routing, it turns out Need to be sequenced, that is, by url , You can fix these by putting string instead of class like
Change
code :
models.ForeignKey(Serial) 
models.ForeignKey("Serial")
How to serialize foreign key from model X in model Y, where model X has the relation to model Y?

How to serialize foreign key from model X in model Y, where model X has the relation to model Y?


By : user3079116
Date : March 29 2020, 07:55 AM
I hope this helps you . You can still query contracts using: register_obj.contract_set.all() which will return all the rows that have the foreign key pointing to the selected register_obj.
And your serializer should be like this:
code :
class RegistrationSerializer(serializers.ModelSerializer):
    client = NameSerializer()
    contracts = SerializerMethodField()

    class Meta:
        model = Registration
        fields = "__all__"

    def get_contracts(self, obj):
        # if you need all the contracts in a list:
        return ContractSerializer(obj.contract_set.all(), many=True).data

        # or if you just need the first one:
        return ContractSerializer(obj.contract_set.first()).data
        # you can also filter the query
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org